Re: Checkout --force without complete overwrite?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Mar 06, 2015 at 02:39:58AM +0000, Phil Hord (hordp) wrote:
> 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.)

Git normally refuses to overwrite uncomitted changes when checking out
complete commits (so not when you check out individual files). --force
overrides that behaviour.

> 
> 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

What about git checkout -m <otherbranch>. This will try to merge the
commit you checkout in the working tree, creating conflicts when they
happen. This way, you can decide per conflict what you want to do, while
non-conflicting changes are just kept.

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]