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

git add myfile.txt

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


当前回答

键入 :

git status

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

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

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

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

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

其他回答

git rm --cached . -r

将“ un- add- add” 添加您从当前目录中添加的所有内容递归到当前目录

只要键入 git 重置, 它就会返回, 并且它就像您自上次承诺以来从未键入过 git 添加 。 请确认您以前已经承诺过 。

吉特拥有每一个可以想象到的行动的指令, 但是它需要广泛的知识才能把事情弄好, 并且因为它充其量是反直觉的...

你以前做过的事:

更改文件并使用 git 添加. , 或 git 添加 < file > 。

你想怎样:

删除索引中的文件, 但保留其版本, 并保留工作副本中未承诺的更改 : git 重置 HEAD < file > 将文件重置为 HEAD 最后一个状态, 撤销更改并将其从索引中删除 : # 思考 ` svn return < file\\ IIRC. git 重置 HEAD < file> git checkout < file > # 如果您有一个像 {file} 这样的\\ 一样的\\ branch}, 使用: git checkout - < file > 这是需要的, 因为 git 重置 -- hard HEAD 将不与单个文件一起工作 。 从索引和版本中删除 < file > , 保留未翻过的文件, 工作副本中的更改 : git rm -cacheed < file > 将 < file > 从工作副本和版本中完全删除: git rm < file > 。

对于特定文件 :

git 重置我的檔案. txt git 檢查我的檔案. txt

对于所有添加的文件 :

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

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

使用 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)
$