git撤销commit到未提交状态

如何把最后一次commit撤销回Changes not staged和Untracked files区呢?

有3种情况:

(1)把最后的commit切回Changes to be committed状态,使用命令:

git reset --soft HEAD^

注意:Windows系统需要在^符号后面两边加上引号,如:

git reset --soft HEAD"^"

(2)把最后的commit切回Changes not staged for commit状态,使用命令:

git reset HEAD^

(3)把Changes to be committed状态切回Changes not staged for commit状态,使用命令:

git reset HEAD <file>...          # 单个文件
git reset HEAD -- .               # 所有Changes to be committed的文件

最后一条命令在git命令行也有提示,不需要记住。

 

参考资料:

  1. Move (or “Undo”) last git commit to unstaged area [duplicate]