Git - 在壁球后恢复中间提交

时间:2016-03-17 10:54:08

标签: git recovery git-squash

我已经将几个提交压缩成一个提交。壁球之前提交的一个提交包括调试打印和后来提交与它一起被删除删除那些打印。 有没有办法恢复它们?

1 个答案:

答案 0 :(得分:8)

是。在壁球前使用git reflog然后git checkout提交哈希。

以下是一系列事件示例。

git init .
touch Foo1 Foo2 Foo3
git add Foo1
git commit -m 'Adding Foo1'
git add Foo2
git commit -m 'Adding Foo2'
git add Foo3
git commit -m 'Adding Foo3'
git log # Observe all three commits
git rebase -i --root # Squash commits

git reflog 
    87a5159 HEAD@{0}: rebase -i (finish): returning to refs/heads/master
    87a5159 HEAD@{1}: rebase -i (squash): Adding Foo1
    a0eecf4 HEAD@{2}: rebase -i (squash): # This is a combination of 2 commits.
    4142aa5 HEAD@{3}: rebase -i (pick): Adding Foo1
    85ad082 HEAD@{4}: rebase -i (pick): Adding Foo1
    cbc3a0c HEAD@{5}: rebase -i (start): checkout cbc3a0c02d1899dcfcc614afc07b3a5a502af56f
    71697f7 HEAD@{6}: commit: Adding Foo3

git checkout HEAD@{6} # get back original commits, with head detached.