是否有可能取消git中最后一个暂存(未提交)的更改?假设当前分支中有很多文件,有些是阶段性的,有些不是。在某个时刻,某个愚蠢的程序员不小心执行了:

git add -- .

...而不是:

git checkout -- .

这个程序员现在可以用一些神奇的git命令取消他最后的更改吗?或者他应该在一开始尝试之前就做出承诺?


当前回答

Remove the file from the index, but keep it versioned and left with uncommitted changes in working copy: git reset head <file> Reset the file to the last state from HEAD, undoing changes and removing them from the index: git reset HEAD <file> git checkout <file> # If you have a `<branch>` named like `<file>`, use: git checkout -- <file> This is needed since git reset --hard HEAD won't work with single files. Remove <file> from index and versioning, keeping the un-versioned file with changes in working copy: git rm --cached <file> Remove <file> from working copy and versioning completely: git rm <file>

其他回答

根据难度的大小和规模,您可以创建一个临时分支,并将当前的工作提交到那里。

然后切换到原始分支并签出,并从从头提交中选择适当的文件。

至少您将拥有当前和以前的状态的永久记录(直到您删除那个scratch分支为止)。

你不能撤销最新的git添加,但是你可以撤销自上次提交以来的所有添加。Git reset不带commit参数会重置索引(取消阶段性变化):

git reset

你可以使用git重置(见文档)

你可以使用

git reset

撤销最近添加的本地文件

 git reset file_name

来撤消对特定文件的更改

Remove the file from the index, but keep it versioned and left with uncommitted changes in working copy: git reset head <file> Reset the file to the last state from HEAD, undoing changes and removing them from the index: git reset HEAD <file> git checkout <file> # If you have a `<branch>` named like `<file>`, use: git checkout -- <file> This is needed since git reset --hard HEAD won't work with single files. Remove <file> from index and versioning, keeping the un-versioned file with changes in working copy: git rm --cached <file> Remove <file> from working copy and versioning completely: git rm <file>