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

git add -- .

...而不是:

git checkout -- .

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


当前回答

我建议使用:

Git rm -r——cached <your_dir>或Git rm——cached <your_file>

它会还原你的

git add <your_dir_or_your_file>

回去,同时保持文件原样。

其他回答

如果你想从git中删除所有添加的文件

git reset

如果要删除单个文件

git rm <file>

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

git reset

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

你可以使用git重置。这将“取消”你上次提交后添加的所有文件。

如果你只想取消一些文件,使用git reset——<file 1> <file 2> <file n>。

此外,还可以使用git reset -p取消文件中的一些更改。

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>