假设有三个commit, git status:
commit3: add test3.c
commit2: add test2.c
commit1: add test1.c
模拟丢失commit记录的情况,执行git reset --hard HEAD~1,删除了commit3,同时test3.c文件已经在working tree里看不到了,如果要恢复commit3,就要使用git reflog和git cherry-pick.
$ git reflog
502dd0f HEAD@{0}: HEAD~1: updating HEAD147b3b5 HEAD@{1}: commit: test3502dd0f HEAD@{2}: commit: test20692c03 HEAD@{3}: commit (initial): test1HEAD@{0}: HEAD~1: updating HEAD
红色加粗的即是被删除了的 commit3,运行git log则没有这一行记录,可以使用git reset --hard 502dd0f 将红色记录删除,恢复cmmit3,需要用git cherry-pick.$ git cherry-pick 147b3b5
[master 02c1e69] test3 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 test3.c运行git log后可以看到:
commit3: add test3.c
commit2: add test2.c
commit1: add test1.c