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

Re: (beginner) git rm

From
Björn Steinbrink <b.steinbrink@gmx.de>
Date
Jan 29, 2009, 02:34 UTC
Message-ID
<20090129023432.GF7503@atjola.homenet>
In-Reply-To
<6bef44ba0901281711m2d05e70fj4dd3ae03d7fe1052@mail.gmail.com>
On 2009.01.28 17:11:07 -0800, Jonathan Wills wrote:
Show 10 quoted lines
> This seems like an appropriate thread to ask something I came across today.
> Either I am unclear about the precise semantics of git checkout <branch>
> <path>, or there is a bug in said command.  I noticed this when I wanted to
> get a directory to match the same directory in another branch, so I did rm
> -rf dir, followed by git checkout master dir.  Afterwards I noticed that
> files in that directory that had previously been in my branch but were not
> in the master branch had returned.  Earlier in this thread it was mentioned
> that git checkout will not remove files, but in this case I had already
> removed those files and git checkout actually replaced them (and not from
> the master branch like I asked, but from the current branch).

This is exactly what I meant. Your "rm -rf dir" only removed the directory from the working tree, but _not_ from the index. And what "git checkout master -- dir" then does is that it puts all the stuff that is in master's "dir" into the index, in _addition_ to the stuff already in the index. And then it puts everything from the index's "dir" into the working tree. This is really a two step process and in each step the pathspec is matched separately.

So the working tree doesn't have "dir" at all.

In the index you still have: whatever dir/file (index version) dir/other_file

In master you have: whatever_2 dir/file (master version) dir/yet_another_file

Then you do "git checkout master -- dir".

In the first step, that "dir" pathspec matches these files from master: dir/file dir/yet_another_file

So those are added to the index, and the index will have: whatever dir/file (master version) dir/other_file dir/yet_another_file

So "dir/file" was replaced, and "dir/yet_another_file" was added. But "dir/other_file" is still around.

And then comes the index -> working tree step. The pathspec matches all three files in "dir" in the index, and so they appear in the working tree.

To get what you expected, you have several options: a) rm -rf dir git add -u dir (drops it from the index) git checkout master -- dir

b) git rm -rf dir git checkout master -- dir

Just saves the "git add -u" step.

c) rm -rf dir git reset master -- dir git checkout -- dir

The reset makes "dir" in the index equal to master's "dir" (ok, technically that's wrong, as the index doesn't even know about "dir" on its own, but my brain fails to produce a correct description).

Björn
Previous: Junio C Hamano
Message 16 of 16 in “(beginner) git rm”
  1. ZabreJan 28, 2009
  2. Peter KreftingJan 28, 2009
  3. ZabreJan 28, 2009
  4. Tomas CarneckyJan 28, 2009
  5. ZabreJan 28, 2009
  6. Tomas CarneckyJan 28, 2009
  7. Theodore TsoJan 28, 2009
  8. ZabreJan 28, 2009
  9. ZabreJan 28, 2009
  10. Björn SteinbrinkJan 28, 2009
  11. ZabreJan 28, 2009
  12. ZabreJan 28, 2009
  13. Junio C HamanoJan 28, 2009
  14. Björn SteinbrinkJan 28, 2009
  15. Junio C HamanoJan 28, 2009
  16. Björn SteinbrinkJan 29, 2009

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.