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

git add myfile.txt

我还没有执行 git 承诺 。 我如何撤销此操作, 使这些更改不被包含在承诺中 ?


当前回答

要澄清: git 添加从当前工作目录向中转区域( index) 移动到中转区域( index) 的移动 。

这个过程叫做中转。 因此,最自然的指令是 进行修改( 更改过的文件) 。 显而易见的是 :

git stage

git 添加只是 Git 阶段的一种更容易到类型化的别名

可惜没有非舞台或不添加命令。 相关命令更难猜测或记住, 但显而易见:

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

其他回答

使用 git 添加 - 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 重置我的檔案. txt git 檢查我的檔案. txt

对于所有添加的文件 :

git 重新设置. git 检查退出 。

注意: 检出将文件的代码更改, 并移动到上次更新( 承诺) 状态。 重置不会更改代码; 它只是重置信头 。

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

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

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

源树使用重置来卸载新文件 。

git add -A

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

git reset

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

类似地,要添加一个特定文件来承诺我们使用 Git 添加 < filename> 的方式相同, 在 unstage( 或重置) 上添加一个我们使用 git 添加的具体文件, 我们使用 git 重置 < filename > 。

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

您想要 :

git rm --cached <added_file_to_undo>

理由:

当我刚开始做这个的时候,我第一次尝试

git reset .

(取消我最初添加的全部内容), 只是为了得到这个(不是这样的)有用信息:

fatal: Failed to resolve 'HEAD' as a valid ref.

结果发现,这是因为 HEAD ref (blanch?) 直到第一次承诺之后才存在。 这就是说,你会遇到跟我一样的初学者的问题, 如果你的工作流程像我的一样, 像是:

cd to my great new project directory to try out Git, the new hotness git init git add . git status ... lots of crap scrolls by ... => Damn, I didn't want to add all of that. google "undo git add" => find Stack Overflow - yay git reset . => fatal: Failed to resolve 'HEAD' as a valid ref.

邮件列表上记录了一个窃听器 记录了这个无益之处

正确的解决方案就在 Git 状态输出中( 是的, 我将它伪装成“ 废话 ” ) 。

... # 将要执行的修改 : # (使用“ git rm -- cashed < file >...” 来取消)...

事实上,解决办法是使用Git rm -- cashed FILE。

在此注意其它处的警告 - git rm 删除您本地的文件工作副本, 但如果您使用 -- cashed , 则不使用。 这是 Git help rm 的结果 :

--cached 使用此选项来卸载和从索引中删除路径。 工作树文件, 不论是否修改, 都会被左移 。

我开始使用

git rm --cached .

以删除所有内容并重新开始。 虽然没有成功, 因为添加. 是循环的, 结果显示 Rm 需要 - r 循环。 叹息 。

git rm -r --cached .

好了,我现在回到我开始的地方。下次我要用...

git add -n .

我把所有东西都拉到一个安全的地方,