有时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
#

当前回答

只使用:

git重置HEAD <文件名>

这将取消该文件并保留您对其所做的更改,因此您可以反过来更改分支,git会将这些文件添加到另一个分支。保留所有更改。

其他回答

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

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)

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

仅适用于版本2.23及以上,

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

我很惊讶没有人提到git reflog (http://git-scm.com/docs/git-reflog):

# git reflog
<find the place before your staged anything>
# git reset HEAD@{1}

reflog是一个git历史记录,它不仅跟踪对repo的更改,还跟踪用户的操作(例如。拉,签出到不同的分支等),并允许撤销这些操作。因此,与其取消被错误地暂存的文件,还不如恢复到没有暂存文件的位置。

这类似于git reset HEAD <file>,但在某些情况下可能更细粒度。

抱歉,我并没有真正回答你的问题,只是指出了另一种方法来取消我经常使用的文件(我非常喜欢Ryan Stewart和waldyrious的答案);)我希望这能有所帮助。

Git rm——cached用于从索引中删除文件。在文件已经在repo的情况下,git rm——cached将从索引中删除该文件,将其留在工作目录中,现在提交也将从repo中删除该文件。基本上,在提交之后,您将取消该文件的版本控制,并保留一个本地副本。

git重置HEAD文件(默认使用——mixed标志)的不同之处在于,在文件已经在repo的情况下,它会用repo (HEAD)中的索引版本替换文件的索引版本,有效地取消对它的修改。

在未版本化文件的情况下,它将取消整个文件,因为该文件不在HEAD中。在这方面,git reset HEAD file和git rm——cached是一样的,但它们是不一样的(就像已经在repo中的文件一样)

对于为什么在git中有两种方法取消文件的问题?-在git中从来没有真正只有一种方法来做任何事情。这就是它的美妙之处:)

取消暂存文件(取消git添加)

将file.js的最后一个版本从repo复制到index

丢弃局部更改

将file.js从索引复制到工作目录

file2.js #恢复工作目录下的多个文件

Git恢复。#丢弃所有本地更改(除了未跟踪的文件)

git clean -fd #删除所有未跟踪的文件