想必你们都知道 git commit --amend
这条实用命令, 其能够用来修改最后一条提交的 commit message, 也能够追加新的修改.
但有时候不当心 amend 了错误的内容, 如何回退呢?
普通青年通常会用 git reset
撤销到上一个提交, 再从新 git commit
一次, 这当然是能够的. 但若是工做区此时已经改的面目全非, 这时若是执行 git reset
, 就很难分的清哪些内容属于被撤销的提交了. 嗯, 这位同窗说这种状况能够用 git stash
来处理, 是能够解决问题的.
可是, 身为文艺青年, 怎么能用这么不优(zhuang)雅(bi)的方法呢.html
先上结论:
若是只 amend 了一次, 那么直接用 git reset HEAD@{1}
就能够撤销此次 amend. 若是 amend 屡次, 就参考 git reflog
进行撤销.git
下面以实例介绍如何就地撤销 git commit --amend
.bash
首先制造事故现场. 追加空行到项目中的 index.html 文件下:ssh
$ echo "" >> index.html $ git add . $ git commit -m "add blank line to index.html"
而后再加一行到 index.html, 并 amend 一下:this
$ echo "this line would break the code" >> index.html $ git add . $ git commit --amend
现场已经出现, 咱们要撤销 amend 的那个提交.code
首先使用 git reflog
命令查看操做记录:htm
$ git reflog c1c1b21 HEAD@{0}: commit (amend): add blank line to index.html 9ff821d HEAD@{1}: commit: add blank line to index.html b078331 HEAD@{2}: commit: no more commit! b86e902 HEAD@{3}: commit: so many commit 77e6ce9 HEAD@{4}: commit: this is another commit ccde039 HEAD@{5}: commit: this is a commit a49dcf4 HEAD@{6}: clone: from ssh://liux@xxx.xx.xx.xxx:29418/git_test.git
看到 amend 操做以前的最后一个操做就是 HEAD@{1}
.
如今能够用 git reset
将当前分支的 HEAD 指向 HEAD@{1}
, 便可达到撤销 amend 的目的:get
$ git reset --soft HEAD@{1} $ git status On branch master Your branch is ahead of 'origin/master' by 5 commits. (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: index.html
随即便用 git status
查看状态, 发现 amend 的内容已经被撤销 (到工做区) 了.
若是想撤销到暂存区, 就用 git reset --soft HEAD@{1}
.
若是想干掉这个修改, 就用 git reset --hard HEAD@{1}
.
这和 git reset
操做 commit 的情形是同样的.it
若是一个 commit 被 amend 了屡次, 也能够用这种方法撤销到任意一次 amend 处:ast
$ git reflog 937fd53 HEAD@{0}: commit (amend): add blank line to index.html 7589755 HEAD@{1}: commit (amend): add blank line to index.html f7ade82 HEAD@{2}: commit (amend): add blank line to index.html c1c1b21 HEAD@{3}: commit (amend): add blank line to index.html 9ff821d HEAD@{4}: commit: add blank line to index.html $ git reset --soft HEAD@{2}
能够看出, 不止是 amend 操做, 其余操做也能够用这种方法进行撤销.
查看分支操做记录: git reflog
重置当前分支 HEAD: git reset