有时git会建议git rm——cached来取消文件,有时git会重置HEAD文件。什么时候用哪个?

D:\code\gt2>git init
Initialized empty Git repository in D:/code/gt2/.git/
D:\code\gt2>touch a

D:\code\gt2>git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       a
nothing added to commit but untracked files present (use "git add" to track)

D:\code\gt2>git add a

D:\code\gt2>git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   a
#
D:\code\gt2>git commit -m a
[master (root-commit) c271e05] a
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a

D:\code\gt2>touch b

D:\code\gt2>git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       b
nothing added to commit but untracked files present (use "git add" to track)

D:\code\gt2>git add b

D:\code\gt2>git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   b
#

当前回答

这个线程有点老了,但我仍然想添加一个小演示,因为它仍然不是一个直观的问题:

me$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   to-be-added
#   modified:   to-be-modified
#   deleted:    to-be-removed
#

me$ git reset -q HEAD to-be-added

    # ok

me$ git reset -q HEAD to-be-modified

    # ok

me$ git reset -q HEAD to-be-removed

    # ok

# or alternatively:

me$ git reset -q HEAD to-be-added to-be-removed to-be-modified

    # ok

me$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   to-be-modified
#   deleted:    to-be-removed
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   to-be-added
no changes added to commit (use "git add" and/or "git commit -a")

git reset HEAD(不带-q)给出关于修改文件的警告,其退出码为1,这将被视为脚本中的错误。

Edit: git签出要修改要删除的HEAD也适用于取消暂存,但会将更改完全从工作空间中删除

更新git 2.23.0:命令会不时发生变化。现在,git状态显示:

  (use "git restore --staged <file>..." to unstage)

... 哪种对这三种变化都有效

其他回答

如果有问题的文件已经在repo中并且处于版本控制之下(以前提交过等),这两个命令有几个微妙的区别:

git reset HEAD <file>取消当前提交的文件。 Git rm——cached <file>将取消该文件以供将来提交。直到git add <file>再次添加它之前,它都是不暂存的。

还有一个更重要的区别:

在运行git rm——cached <file>并将你的分支推到远程,任何从远程拉你的分支的人都会从他们的文件夹中实际删除这个文件,即使在你的本地工作集中,这个文件只是变得不被跟踪(即没有从文件夹中物理删除)。

最后一个区别对于包含配置文件的项目很重要,其中团队中的每个开发人员都有不同的配置(即不同的基本url, ip或端口设置),所以如果你使用git rm——cached <file>,任何拉你的分支的人都必须手动重新创建配置,或者你可以将你的配置发送给他们,他们可以重新编辑回他们的ip设置(等),因为删除只会影响从远程拉你的分支的人。

>并不取消文件,它实际上是从repo中删除文件(假设它之前已经提交),但将文件留在你的工作树中(留下一个未跟踪的文件)。

git reset——<filePath>将取消对给定文件的任何阶段性更改。

也就是说,如果您使用缓存在一个新文件上的git rm—,它基本上看起来就像您刚刚取消了它的暂存,因为它之前从未提交过。

更新git 2.24 在这个新版本的git中,你可以使用git restore - staging而不是git reset。 参见git文档。

仅适用于版本2.23及以上,

除了这些建议,你可以使用 Git恢复—staging <文件>,以便取消文件的阶段。

假设你通过git add <folder>暂存整个目录,但是你想从暂存列表(即运行git状态时生成的列表)中排除一个文件,并将修改保留在排除的文件中(你正在做一些事情,它还没有准备好提交,但你不想失去你的工作…)你可以简单地使用:

Git重置<文件>

当您运行git status时,您将看到您重置的任何文件都是非暂存的,而您添加的其余文件仍然在暂存列表中。

在> 2.2的新版本中,你可以使用git restore——staging <file_name>。 请注意这里 如果你想一次取消(移动到更改)你的文件,你可以使用上面的命令和你的文件名。如

git restore --staged abc.html

现在,如果您想一次性取消所有文件,您可以这样做

git restore --staged .

请注意空格和点(.)表示考虑将所有文件分段。