简体   繁体   English

git rm --cached 和 git reset 有什么区别<file> ?</file>

[英]What is the difference between git rm --cached and git reset <file>?

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 resource unstaging a file is done with但是根据这个资源取消暂存文件是用

git reset HEAD <file>

What is the difference?有什么不同? Is there one?有吗?

With git rm --cached you stage a file for removal, but you don't remove it from the working dir. 使用git rm --cached您可以git rm --cached要删除的文件,但不要将其从工作目录中删除。 The file will then be shown as untracked. 然后该文件将显示为未跟踪。

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. 使用git reset <file>您可以取消git reset <file>文件。 In the example above you might want to use git reset test to unstage the removal. 在上面的示例中,您可能希望使用git reset test来取消删除。

git reset test
git status
On branch master
nothing to commit, working directory clean

git rm --cached removes the file from the index but leaves it in the working directory. git rm --cached从索引中删除文件,但将其保留在工作目录中。 This indicates to Git that you don't want to track the file any more. 这向Git表明您不想再跟踪该文件。

git reset HEAD leaves the file as a tracked file in the index, but the modifications cached in the index are lost. git reset HEAD将文件保留为索引中的跟踪文件,但索引中缓存的修改将丢失。 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) 这具有如下效果:好像缓存中的文件已被HEAD中的文件覆盖(并且工作树文件未被触及)

clear cache full by this:通过此清除缓存已满:

git rm -r --cached. git rm -r --缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM