使用git revert
命令取消已经committed
的操作。举例如下:
[root@localhost git_repo]# git log --oneline
4a95041 Add a crazzzy experiment
5f08b5f Add navigation links
7f9fa70 Create blue and orange pages
7807520 Commit the first version of index.html.
如果想取消最后一次commit
(commit ID
是4a95041
),使用git revert 4a95041
命令:
[root@localhost git_repo]# git revert 4a95041
[master 0b959a0] Revert "Add a crazzzy experiment"
Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 14 deletions(-)
delete mode 100644 crazy.html
[root@localhost git_repo]# git log --oneline
0b959a0 Revert "Add a crazzzy experiment"
4a95041 Add a crazzzy experiment
5f08b5f Add navigation links
7f9fa70 Create blue and orange pages
7807520 Commit the first version of index.html.
现在第三次和第五次commit
代表相同的snapshot
。并且第四次commit
还在revision history
中,以后也可以恢复到第四次commit
的内容。要注意,git revert
命令使用的commit ID
是要取消操作的commit ID
,不是要“回滚”到的commit ID
。
取消还没committed
的操作分以下两种情况:
(1)Tracked
文件:git reset --hard
命令会把所有tracked
文件恢复到最近一次committed
状态。加上--hard
选项会真正更新文件,否则只是unstage
文件,但文件的内容还是变化了。
(2)Untracked
文件:git clean -f
会删除所有untracked
文件。
git reset
取消working directory
和staged snapshot
的操作,而git revert
是取消committed snapshots
的操作。如下图所示:
参考资料:
Undoing Changes。