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

git add myfile.txt

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


当前回答

运行运行中

git gui

,然后手动删除所有文件,或者选择全部文件,然后单击实施后的非阶段按钮。

其他回答

只是类型git reset它将复返,犹如你从未打过字一样。git add .自从你最后一次承诺之后,你一定要保证你以前的承诺。

更新2019年更新

正如其他人在相关问题中所指出的(见A/CN.9/WG.III/WG.II),在这里, 在这里, 在这里, 在这里, 在这里, 在这里, 和在这里现在可以了取消一个单独的文件与:

git restore --staged <file>

取消所有文件以下列方式写成:

git restore --staged .

Notes

git restore2019年7月以2.23版本发布。
--staged旗帜, 它会恢复索引的内容( 在此询问什么 ) 。

运行时git status与 Saged 未经发送文件一起, Git 现在建议用这个来解开 Page 文件( 而不是git reset HEAD <file>在 v2.23 之前使用过。

使用使用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 add -A

用于将所有文件添加到您的承诺( 阶段) 。

git reset

是“ git 添加 - A” 命令的对面。它会删除所有要执行的分阶段文件( 手段已准备就绪 ) 。

同样,为了添加一个用于承诺的具体文件,我们使用git add <filename>相同的方式, to unstage( 或重置) 一个我们添加的具体文件git add我们使用git reset <filename> .

git 重置与 git 添加完全相反 。

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

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版总版)