git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Checkout --force without complete overwrite?
@ 2015-03-06  2:39 Phil Hord (hordp)
  2015-03-06 11:36 ` Kevin D
  0 siblings, 1 reply; 2+ messages in thread
From: Phil Hord (hordp) @ 2015-03-06  2:39 UTC (permalink / raw)
  To: git@vger.kernel.org

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Checkout --force without complete overwrite?
  2015-03-06  2:39 Checkout --force without complete overwrite? Phil Hord (hordp)
@ 2015-03-06 11:36 ` Kevin D
  0 siblings, 0 replies; 2+ messages in thread
From: Kevin D @ 2015-03-06 11:36 UTC (permalink / raw)
  To: Phil Hord (hordp); +Cc: git

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-03-06 11:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-06  2:39 Checkout --force without complete overwrite? Phil Hord (hordp)
2015-03-06 11:36 ` Kevin D

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).