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

git add myfile.txt

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


当前回答

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

其他回答

您可以使用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 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 -i

选择 选项 3 到 取消 添加文件 。 在我的情况中, 我通常要添加多个文件, 用交互模式, 您可以使用这样的数字来添加文件 。 这将除 4: 1, 1, 2, 3, 和 5 以外, 全部要用这个数字来添加文件 : 1, 2, 3 和 5

要选择一个序列, 只要类型 1-5 就可以从 1 到 5 全部取出 。

Git 中转文件

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

git reset *

也许Git已经进化了 自从你提出问题之后

$> git --version
git version 1.6.2.1

现在,你可以试试:

git reset HEAD .

这应该就是你要找的