git rm --cached 和 git reset <file> 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38001223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between git rm --cached and git reset <file>?
提问by sakurashinken
According to the git rm documentation,
根据git 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.
But according to this resourceunstaging a file is done with
但是根据此资源取消暂存文件已完成
git reset HEAD <file>
What is the difference? Is there one?
有什么不同?有吗?
采纳答案by René Link
With git rm --cached
you stage a file for removal, but you don't remove it from the working dir. The file will then be shown as untracked.
随着git rm --cached
您暂存要删除的文件,但您不会将其从工作目录中删除。然后该文件将显示为未跟踪。
Take a test drive
试驾
git init test_repo
cd test_repo
touch test
git add test
git commit -m 'Added file test
git rm --cached test
git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: test <---- staged for removal
Untracked files:
(use "git add <file>..." to include in what will be committed)
test <-- still in the working dir
With git reset <file>
you can unstage a file. In the example above you might want to use git reset test
to unstage the removal.
有了git reset <file>
你可以unstage文件。在上面的示例中,您可能想要用来git reset test
取消暂存删除。
git reset test
git status
On branch master
nothing to commit, working directory clean
回答by Gregg
git rm --cached removes the file from the index but leaves it in the working directory. This indicates to Git that you don't want to track the file any more.
git rm --cached 从索引中删除文件,但将其保留在工作目录中。这向 Git 表明您不想再跟踪该文件。
git reset HEAD leaves the file as a tracked file in the index, but the modifications cached in the index are lost. This has the effect as if the file in cache had been over written by the file in HEAD (and the working tree file being untouched)
git reset HEAD 将文件作为索引中的跟踪文件保留,但索引中缓存的修改会丢失。这就像缓存中的文件已被 HEAD 中的文件覆盖(并且工作树文件未受影响)