I have a repo whose workdir tends to get pretty dirty as I jump around from
branch to branch tending weeds and whatnot. Sometimes when I try to switch
branches git refuses because of local file changes.
git checkout otherbranch
error: Your local changes to the following files would be overwritten by
checkout:
foo.txt
bar.c
Please, commit your changes or stash them before you can switch branches.
Aborting
I resolve this by examining my local changes and deciding if I want to keep
them or not. I keep the changes in the conflicting files that I want, and then
I discard the rest.
One way to "discard the rest" is to say
git checkout HEAD -- foo.txt bar.c && git checkout otherbranch
But sometimes the list of local-change conflicts I want to discard is too long
and this recipe seems like a good alternative to me:
git checkout -f otherbranch
But this is disastrous, because I have been focused on examining the
conflicting local changes in foo.txt and bar.c, but I forgot about my
non-conflicting changes to baz.c, lost as it is in a sea of untracked files
making up the majority of my workdir local changes. So all my untracked files
make the leap unscathed, but my precious forgotten changes in baz.c get wiped
out by the checkout --force, even though the baz.c in index and in otherbranch
are the same.
I've read the documentation for 'git checkout --force' several times and I have
a hard time deciding what it means to do. But I'm sure it's doing what it's
designed to do and what the man page says it will. (English is my first
language, btw.)
What I am seeking is some way to checkout the other branch and replace my
conflicted local changes while ignoring my non-conflicting local changes in
tracked files. Something like --force-gently, maybe. Does such an option
exist?
I could script something, but it comes up only often enough to bite me, so I'm
sure I'd forget I had scripted it.
Thanks,
Phil
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html