1. 撤銷 git add
如果是撤銷所有的已經add的文件:
git reset HEAD -- .
如果是撤銷某個文件或文件夾:
git reset HEAD -- filename
2. 撤銷 git commit
git reset --soft HEAD^
HEAD^的意思是上一個版本,也可以寫成HEAD~1
如果你進行了2次commit,想都撤回,可以使用HEAD~2
其他參數解析:
-
--soft
不刪除工作空間改動代碼,撤銷commit,不撤銷git add . -
--mixed
不刪除工作空間改動代碼,撤銷commit,並且撤銷git add .
這個為默認參數, git reset --mixed HEAD^ 和 git reset HEAD^ 效果是一樣的。 -
--hard
刪除工作空間改動代碼,撤銷commit,並且撤銷git add .
補充:如果該倉庫到目前為止只有commit過一次代碼,則會報錯:
$ git reset HEAD^
fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'