撤销开始

如果我们忘记了被覆盖的那个commit id,那就用reflog命令看一下

$ git reflog
6889e84 (HEAD -> master) HEAD@{0}: commit (amend): modified 1/2/3.txt
b82585f HEAD@{1}: commit: modified 1/2.txt

$ git reset b82585f
Unstaged changes after reset:
M       3.txt

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   3.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git log
commit b82585f65b4c467a7e5855191b73a613fcf20892 (HEAD -> master)
Author: ares.wang <ares.wang@seraphic-corp.com>
Date:   Thu Sep 14 14:18:40 2017 +0800

    modified 1/2.txt 

可以看出,reset已经恢复到上一个被覆盖的commit id了,且对文件的修改回退到代码仓not staged的状态了

不使用 git reset --hard的目的就是为了保留本地修改,否则修改就会被丢弃!演示如下:

$ git reset --hard b82585f
HEAD is now at b82585f modified 1/2.txt

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean