git rm cached和git reset HEAD的区别



我猜我和git rm --cached搞混了。
我已经提交了一个存储库和一个文件。我修改文件,我这样做:git add myfile
该文件现在已被分级。
当我做git status时:

# On branch master   
# Changes to be committed:  
#   (use "git reset HEAD <file>..." to unstage)  
#  
#       modified:   com/main/StringMain.java  
#  

现在该文件是修改后的跟踪文件。所以我猜那是在集结区。所以我不能理解(use "git reset HEAD <file>..." to unstage)的建议是什么意思。所以我做了:git rm --cached而不是git commit。但这似乎删除了我的文件被跟踪,使其不被跟踪。
如果我输入git status:

# On branch master  
# Untracked files:  
#   (use "git add <file>..." to include in what will be committed)  
#  
#       com/  
nothing added to commit but untracked files present (use "git add" to track)  

发生了什么?

可以这样想:

git rm --cached [file]

这只是从被跟踪的文件中删除一个文件(文件被添加后的状态-即git add [file])

git reset HEAD [file]

这只是继续跟踪对文件的更改,但会将其放回"未分级"区域。

下面是一个例子。

首先,我创建了4个未跟踪的文件:

$ for i in {A..D}; do touch $i; echo "First line" > $i; done
$ ls
A   B   C   D
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    A
    B
    C
    D
nothing added to commit but untracked files present (use "git add" to track)

接下来,我使用git add跟踪它们,然后我将提交更改:

$ git add .
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    new file:   A
    new file:   B
    new file:   C
    new file:   D
$ git commit -m "First Commit"
[master 6e8d625] First Commit
 4 files changed, 4 insertions(+)
 create mode 100644 A
 create mode 100644 B
 create mode 100644 C
 create mode 100644 D
$ git status
On branch master
nothing to commit, working directory clean

现在我将修改文件A,以便git拾取更改,然后我将更改后的文件添加到暂存区:

$ echo "First line of file A" > A
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   A
no changes added to commit (use "git add" and/or "git commit -a")
$ git add A
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    modified:   A

这就是区别的所在

第一个例子是当你使用git rm——cached: 时发生的情况
$ git rm --cached A
rm 'A'
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    deleted:    A
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    A

注意文件A现在是如何不被跟踪的,就像一开始将它添加到git之前的情况一样(当使用"git add ."时)。

现在,第二个例子是如果我要使用git reset HEAD代替:

$ git reset HEAD A
Unstaged changes after reset:
M   A
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   A
no changes added to commit (use "git add" and/or "git commit -a")

这里您将注意到它将文件A的状态重置回未分级状态,但仍然继续跟踪它的更改。

git rm --cached从索引中删除文件。

git reset HEAD将文件的索引版本重置为HEAD提交时的状态。

所以不同之处在于,第一个命令删除文件,而第二个命令将其恢复到最后提交的版本。


为了验证这一点,您可以使用git diff将工作树与索引进行比较,使用git diff --cached将索引与头提交进行比较。

当您运行git rm --cached时,修改的文件将完全从索引中删除。它仍然存在于工作目录和最后一次提交中。如果您将索引与上次提交进行比较:

git diff --cached modified_file

您将看到修改后的文件不在索引中。

证实了这一点
git status

这将显示文件被计划在提交时删除。您的工作目录不受git rm --cached的影响,因为--cached直接在索引中工作。

假设你删除一个文件从舞台区(git rm文件)所以删除文件从你的工作directoy和暂存区域,现在让我们再次删除它但是你上演后,所以你有了但不是在你的工作目录,如果你使用git rm——缓存文件,你的文件回到你的工作目录(git也不跟踪了,它不会告诉你阶段),但如果你使用git重置的头文件,该文件将从舞台区删除,而不返回到您的工作目录。

最新更新