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

Re: git checkout -f branch doesn't remove extra files

From
Junio C Hamano <junkio@cox.net>
Date
Aug 12, 2005, 20:27 UTC
Message-ID
<7v8xz6uchu.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<B8E391BBE9FE384DAA4C5C003888BE6F04214AC7@scsmsx401.amr.corp.intel.com>
"Luck, Tony" <tony.luck@intel.com> writes:
Show 9 quoted lines
> I see that when I switch view to a different
> branch with:
>
> 	$ git checkout -f someoldbranch
>
> that any files that exist in my previous branch view
> but not in "someoldbranch" are not deleted.
>
> ... I wondered whether this was a deliberate choice
Not really.

When possible, please use "git checkout" without -f. That form uses "git-read-tree -u -m", which can tell from your current index file what's changed and what's going away. This is only possible if read-tree can rely on what is in your index file actually matches the status of your working tree, and the -f flag to checkout is to work around cases where you cannot give that guarantee to read-tree and would want/need to start over from scratch. With -f flag, you are telling git-read-tree to ignore what is currently recorded in your index file, hence what should be in your working tree before checkout. There is no way for git-read-tree to tell which are source files that exist only in the old thing you had checked out before (i.e. should be removed) and which are files your build procedure left and/or files you made by hand outside of SCM control (i.e. should be kept) without that information, so that is why those files were left behind.

Previous: Luck, Tony
Message 2 of 2 in “git checkout -f branch doesn't remove extra files”
  1. Luck, TonyAug 12, 2005
  2. Junio C HamanoAug 12, 2005

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.