git 删除了本不应该删除的commit 如何恢复

参考链接:https://blog.csdn.net/shichaosong/article/details/22067529

git reflog 和git log的区别,外加git cherry-pick的一种用法

 

 

git reflog 可以查看所有分支的所有操作记录(包括(包括commit和reset的操作),包括已经被删除的commit记录,git log则不能察看已经删除了的commit记录,而且跟进结果可以回退道某一个修改:

xxx@xxx-OptiPlex-9010:~/dev/Jellebean/packages/apps/Update$ git reflog 
f6b7db5 HEAD@{0}: cherry-pick: New Userbook.
102a67b HEAD@{1}: checkout: moving from new_branch_name to for-upload
b077c1e HEAD@{2}: checkout: moving from for-upload to new_branch_name
102a67b HEAD@{3}: checkout: moving from b077c1e5db8cf32cf42b201d211a340493348348 to for-upload
b077c1e HEAD@{4}: commit (amend): New Userbook.
51ed395 HEAD@{5}: commit (amend): New Userbook.
1def615 HEAD@{6}: commit (amend): New Userbook.
c1c3262 HEAD@{7}: reset: moving to HEAD~1


具体一个例子,假设有三个commit, git st:

commit3: add test3.c

commit2: add test2.c

commit1: add test1.c

如果执行git reset --hard HEAD~1则 删除了commit3,如果发现删除错误了,需要恢复commit3,这个时候就要使用git reflog

 HEAD@{0}: HEAD~1: updating HEAD
63ee781 HEAD@{1}: commit: test3:q
红色加粗的即是被删除了的 commit3,运行git log则没有这一行记录

可以使用git reset --hard 63ee781将红色记录删除,则恢复了cmmit3,运行git log后可以看到:

commit3: add test3.c

commit2: add test2.c

commit1: add test1.c

这里也可以使用另外一种方法来实现:git cherry-pick 63ee781

 

posted @ 2018-07-19 12:32  TXFSheng  阅读(542)  评论(0编辑  收藏  举报