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

git add myfile.txt

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


当前回答

如果您想要还原最后一个任务, 但仍要保留在任务中本地作出的更改, 请使用此命令 :

git reset HEAD~1 --mixed

其他回答

使用使用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将当前工作目录的更改移动到中转区(索引)。

此进程被称为此进程中 中 中。因此,最自然的命令阶段更改(已修改的文件)是显而易见的:

git stage

git add是一个简单到类型化的别名git stage

可惜没有git unstagegit unadd命令。相关命令更难猜或记,但很明显:

git reset HEAD --

我们可以很容易地为此创建别名:

git config --global alias.unadd 'reset HEAD --'
git config --global alias.unstage 'reset HEAD --'

最后,我们有了新的命令:

git add file1
git stage file2
git unadd file2
git unstage file1

我个人甚至使用更短的化名:

git a # For staging
git u # For unstaging

假设我创造了一个新的文件,newFile.txt:

Enter image description here

假设我无意中添加文件git add newFile.txt:

Enter image description here

现在我想撤销此添加, 在承诺之前,git reset newFile.txt:

Enter image description here

键入 :

git status

Git会告诉你什么是舞台,等等, 包括关于如何解除舞台的指示:

use "git reset HEAD <file>..." to unstage

我发现吉特做的相当不错 裸体我做正确的事情 在这样的情况下。

注:最近Git版本(1.8.4.x)更改了此电文:

(use "git rm --cached <file>..." to unstage)

此命令将卸载您的更改 :

git reset HEAD filename.txt

您也可以使用

git add -p 

以添加文件的部分。