git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Checkout --force without complete overwrite?

From
Kevin D <me@ikke.info>
Date
Mar 6, 2015, 11:36 UTC
Message-ID
<20150306113636.GA11836@vps892.directvps.nl>
In-Reply-To
<D289021BF8782144A9BEB8ED592B380D2B106BA9@xmb-aln-x02.cisco.com>
On Fri, Mar 06, 2015 at 02:39:58AM +0000, Phil Hord (hordp) wrote:
Show 20 quoted lines
> 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.

Show 7 quoted lines
> 
> 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.

Previous: Phil Hord (hordp)
Message 2 of 2 in “Checkout --force without complete overwrite?”
  1. Phil Hord (hordp)Mar 6, 2015
  2. Kevin DMar 6, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.