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

git add myfile.txt

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


当前回答

git reset filename.txt  

将会从当前索引“ 即将被执行” 区域中删除一个名为文件名. txt 的文件, 而不更改其它内容 。

其他回答

您可以使用g基基边命令或图形用户界面 Git 。

单个文件

git reset File.txt

多个文件

git reset File1.txt File2.txt File3.txt

示例示例示例示例

假设你添加了Home.js 居家公居, 项目表.js, 最新情况.js犯错,

Enter image description here

并要撤消/重置

git reset src/components/home/Home.js src/components/listItem/ListItem.js src/components/update/Update.js

Enter image description here

同一例子使用Git 图形界面

git gui

打开窗口。不检查您的文件分阶段变化(将承诺)

Enter image description here

撤消撤消已经添加的文件使用 Git 很容易。 用于重设myfile.txt已经添加的,使用:

git reset HEAD myfile.txt

解释:

启动不需要的文件后,要撤消文件,您可以完成git reset. Head是在本地文件中您文件的头,而最后一个参数是您文件的名称。

我为你们创建了以下图像中的步骤, 更详细地介绍了这些步骤, 包括在这些情况下可能发生的所有步骤:

git reset HEAD file

更新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 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 -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)
$