Git撤销最后一次提交
Git可以使用reset重置来撤销提交。
方法一
撤销最后一次提交
<pre data-language-edit="Bash" spellcheck="false">```bash
git reset HEAD~1
执行后,状态重置为上一次提交,且撤回提交的文件的状态变回unstaged,即文件没有被git跟踪。
示例
<pre data-language-edit="Bash" spellcheck="false">```bash $ git commit -m <span class="hljs">'add test.html'</span> [master ade6d7e] add test.html 1 file changed, 1 insertion(+) create mode 100644 test.html $ git reset HEAD~1 $ git status On branch master Untracked files: (use <span class="hljs">"git add <file>..."</span> to include <span class="hljs">in</span> what will be committed)
test.html
nothing added to commit but untracked files present (use <span class="hljs">"git add"</span> to track)
撤回后test.html为Untracked files。
方法二
<pre data-language-edit="Bash" spellcheck="false">```bash
git reset --soft HEAD~1
使用--soft,执行后,状态重置为上一次提交,但撤回提交的文件add到git,被git跟踪。
示例
<pre data-language-edit="Bash" spellcheck="false">```bash $ git commit -m <span class="hljs">'add test.html'</span> [master 877b8f0] add test.html 1 file changed, 1 insertion(+) create mode 100644 test.html
$ git reset --soft HEAD~1
clcaza@clcaza MINGW64 /d/webstormProjectsDemo/ngcli-demo (master) $ git status On branch master Changes to be committed: (use <span class="hljs">"git reset HEAD <file>..."</span> to unstage)
new file: test.html
test.html状态为Changes to be committed