這三個命令均可以用於撤銷。git
reset和checkout能夠做用於commit或者文件,revert只能做用於commit。安全
$ git checkout hotfix $ git reset HEAD~2
git reset用於撤銷未被提交到remote的改動,即撤銷local的修改。除了移動當前分支的HEAD,還能夠更改workspace和index:spa
--soft
:修改HEAD,不修改index和workspace。指針
--mixed
:修改HEAD和index,不修改workspace。默認行爲。code
--hard
:修改HEAD、index、workspace。rem
git reset --mixed HEAD
把index的內容退回到workspace中。git reset --hard HEAD
把index和workspace的修改所有撤銷。it
checkout做用於commit級別時,只是移動HEAD到不一樣的commit。若是有unstaged的文件,git會阻止操做並提示。若是使用commit id做爲參數,可能會致使野指針。table
$ git checkout hotfix $ git revert HEAD^^
revert經過新建一個commit來撤銷一次commit所作的修改,是一種安全的方式,並無修改commit history。class
revert用於撤銷committed changes,reset用於撤銷uncommitted changes。file
git reset <commit> <filename>
只修改index去匹配某次commit。
git reset HEAD filename
把文件從index退回workspace,並將更改保存在workspace中。
git checkout <commit> <filename>
只修改workspace去匹配某次commit。
git checkout HEAD filename
抹掉文件在workspace的修改。
Command | Scope | Common use cases |
---|---|---|
git reset | Commit-level | Discard commits in a private branch or throw away uncommited changes |
git reset | File-level | Unstage a file |
git checkout | Commit-level | Switch between branches or inspect old snapshots |
git checkout | File-level | Discard changes in the working directory |
git revert | Commit-level | Undo commits in a public branch |
git revert | File-level | (N/A) |