我使用命令错误地将文件添加到 Git :
git add myfile.txt
我还没跑呢git commit
。我如何撤销这些修改,使这些修改不包含在承诺中?
我使用命令错误地将文件添加到 Git :
git add myfile.txt
我还没跑呢git commit
。我如何撤销这些修改,使这些修改不包含在承诺中?
当前回答
要撤销git add
, 使用 :
git reset filename
其他回答
问题未明确提出,原因是:git add
有两个含义:
git rm --cached file
.git reset HEAD file
.如果有疑问,使用
git reset HEAD file
因为它在两种情况下都做了预期的事
警告 :如果有的话git rm --cached file
档案中修改后(一个之前在存储库中存在的文件) , 然后文件将会在git commit
! 它将仍然存在于您的文件系统中, 但是如果有其他人拉动您的承诺, 文件将会从他们的工作树上删除 。
git status
将会告诉你,如果文件是新文件或修改后:
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: my_new_file.txt
modified: my_modified_file.txt
使用使用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 reset filename
运行运行中
git gui
,然后手动删除所有文件,或者选择全部文件,然后单击实施后的非阶段按钮。
如上文所述,从中转区删除新文件(仅在出现新文件的情况下):
git rm --cached FILE
使用 rm -- -- 刻录仅用于新文件不小心添加 。