我使用命令错误地将文件添加到 Git :

git add myfile.txt

我还没跑呢git commit。我如何撤销这些修改,使这些修改不包含在承诺中?


当前回答

请注意,如果您没有指定修改,则必须包含一个分隔符。例如,我的控制台:

git reset <path_to_file>
fatal: ambiguous argument '<path_to_file>': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

git reset -- <path_to_file>
Unstaged changes after reset:
M    <path_to_file>

(1.7.5.4版总版)

其他回答

要撤销git add, 使用 :

git reset filename

使用使用git add -i以删除您即将承诺的添加文件 。 例如 :

添加您不想添加的文件 :

$ git add foo
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   foo
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
# [...]#

进入交互式添加以取消您的添加( 这里输入的 git 命令是“ r”( revert) , “ 1”( 列表返回显示的第一个条目) , “ return” 退出返回模式, 以及“ q” ( quit) :

$ git add -i
           staged     unstaged path
  1:        +1/-0      nothing foo

*** Commands ***
  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
What now> r
           staged     unstaged path
  1:        +1/-0      nothing [f]oo
Revert>> 1
           staged     unstaged path
* 1:        +1/-0      nothing [f]oo
Revert>> 
note: foo is untracked now.
reverted one path

*** Commands ***
  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
What now> q
Bye.
$

这就是你的证据 证明"foo"又回到了未追踪的名单上

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

要重置特定文件夹( 及其子文件夹) 中的每一文件, 您可以使用以下命令 :

git reset *

在源树中,您可以通过 GUI 轻松地做到这一点。您可以检查源树命令用什么来取消文件 。

我创建了一个新的文件, 并将其添加到 Git 中。 然后我用“ 原始树 GUI” 解除了它。 这是结果 :

解除文件 [08/12/ 15 10: 43] git - c diff. mnemonicprefix= false - c c c核心. cootepath= false - creedical. helper= 源树重新设置 -q - 路径/ 到/ 文件/ files/ filename.java

源树用途reset以卸载新文件。

最直觉的解决方案之一是使用原始树.

您可以从舞台和非舞台拖放文件或拖放文件

Enter image description here