![](/img/trans.png)
[英]What is the practical difference between `git rm --cached`, `git reset --` and `git reset HEAD` to unstage changes?
[英]Difference between git rm cached and 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 文件)中删除了一个文件,因此它会从您的工作目录和暂存区中删除该文件,现在让我们再次将其删除,但在暂存之后,因此您将其暂存但不在您的暂存区中工作目录,如果你使用 git rm --cached file ,你会把你的文件放回到你的工作目录(而且 git 不再跟踪它,它不会告诉你暂存它),但是如果你使用 git reset HEAD 文件,文件已从舞台区域中删除,而无需返回您的工作目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.