Re: (beginner) git rm

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

 



On 2009.01.28 17:11:07 -0800, Jonathan Wills wrote:
> 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
--
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]

  Powered by Linux