Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - recover intermediate commits after squash

I have squashed several commits into one commit. One of the commits before the squash included debug prints and a later commit that was squashed together with it removed those prints. Is there any way to recover them?

like image 250
tba Avatar asked Mar 17 '16 10:03

tba


1 Answers

Yes. Use git reflog and then git checkout the commit hash right before the squash.

Here's an example sequence of events.

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.
like image 147
merlin2011 Avatar answered Sep 17 '22 12:09

merlin2011