为什么要“git rm --cached”删除最新的文件? [英] Why "git rm --cached" remove the up-to-date files?
问题描述
我在本地git仓库中有一个README.md文件。当地的git仓库没有任何阶段性的工作树,没有任何变化。请参阅下面的内容:
$ git status
在分支大师
您的分支是最新的起源/主。
没有提交,工作树干净
然后我运行 git rm --cached - README.md
,它表示:
$ git rm - 缓存 - README.md
rm'README.md'
根据 git help rm
:
- 缓存
使用此选项可以取消并仅从索引删除路径 。工作树文件无论是否被修改,都将被单独保留。
README.md没有阶段性更改。所以暂存区/索引应该不包含任何内容。为什么它仍然被删除?
ADD1
基于@ DietrichEpp的回答:
状态存在于git所关注的3个位置:
- 工作树
- index
- repo
Git决定什么是通过比较1和2来跟踪
或 un-staged
;
Git决定什么是 to-be-commited
通过比较2和3;
git reset
可用于更改1和/或2中的状态。
索引不包含任何内容,包含您将要提交的任何内容,包括已经是回购协议一部分的文件。假设您运行以下命令:
touch a.txt b.txt
git add a.txt b .txt
git commit
git rm --cached a.txt
git commit
当您第一次运行 git add
时,它会添加 a.txt
和 b.txt
到索引。然后, git commit
接受索引中的任何内容并提交它。
索引仍然包含 a.txt
和 b.txt
。
当您< a.txt
,但是 b.txt
仍然在索引中。然后, git commit
接受索引中的任何内容(只有 b.txt
)并提交它。 a.txt
文件仍然存在,因为 git rm --cached
不会触及实际文件,并且 git commit
。
因为第一次提交包含 a.txt
,第二次提交不会,这显示为删除。
最主要的是 git status
会显示索引和磁盘上实际文件之间的区别。因此,如果 git status
为空,那么磁盘上所有未忽略的文件也都位于索引中。
I have a README.md in my local git repo. The local git repo has nothing staged and nothing changed in working tree. See below:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
Then I run git rm --cached -- README.md
, it says this:
$ git rm --cached -- README.md
rm 'README.md'
According to the git help rm
:
--cached
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
There's no staged changes about README.md. So the staging area/index should contain nothing. Why is it still removed?
ADD1
Based on @DietrichEpp's answer:
States exist at 3 places as git is concerned:
- working tree
- index
- repo
Git decides what's un-tracked
or un-staged
by comparing 1 and 2;
Git decides what's to-be-commited
by comparing 2 and 3;
git reset
can be used to change state in 1 and/or 2.
The index doesn't contain nothing, it contains whatever you are going to commit, including the files that are already part of the repo. Let's say that you run the following commands:
touch a.txt b.txt
git add a.txt b.txt
git commit
git rm --cached a.txt
git commit
When you first run git add
, it adds a.txt
and b.txt
to the index. Then, git commit
takes whatever is in the index and commits it.
The index still contains a.txt
and b.txt
.
When you git rm --cached
, it removes a.txt
from the index, but b.txt
is still in the index. Then, git commit
takes whatever is in the index (only b.txt
) and commits it. The a.txt
file is still there, because git rm --cached
doesn't touch the actual file, and neither does git commit
.
Because the first commit contains a.txt
and the second commit does not, this shows up as a deletion.
The main thing that git status
does is show you the difference between the index and the actual files on disk. So if git status
is empty, then all of the non-ignored files on disk are in the index, too.
这篇关于为什么要“git rm --cached”删除最新的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!