Friday, 1 June 2012

Being too clever in Git and then some

Everybody already knows about commit squashing in Git. What's less known is the new --autosquash feature in Git 1.7. There is a nice article about this in Technosorcery. What I do wonder about this is the whole point of "squash" because you need to have exactly the same comment (or start of) as the one you want to squash into.

So the difference between squash and fixup is that the latter doesn't add the same commit message twice, so why would I want to use squash? It would make sense if it could have some other message, now you probably just delete the message?

Well, so I was doing some squashing in a repository when I managed to mess things up. It was early in the morning, mind.. what I did was to squash commits to a commit I had already published, and this is obviously a big no-no.

Thankfully for me, in Git you can fix your mistakes.

$:~/src/dsp_prod$ git reflog
73ded3c HEAD@{0}: ORIG_HEAD: updating HEAD
1813552 HEAD@{1}: commit: My really important commit I have already published!
9e63b6e HEAD@{2}: rebase -i (fixup): updating HEAD
73ded3c HEAD@{3}: commit: # This is a combination of 3 commits.
9e63b6e HEAD@{4}: rebase -i (fixup): updating HEAD
6d1b4bb HEAD@{5}: commit: # This is a combination of 2 commits.
9e63b6e HEAD@{6}: rebase -i (fixup): updating HEAD
46d9505 HEAD@{7}: checkout: moving from master to 46d9505
5159b02 HEAD@{8}: checkout: moving from master to 5159b02
5159b02 HEAD@{9}: checkout: moving from master to 5159b02
5159b02 HEAD@{10}: checkout: moving from master to 5159b02
5159b02 HEAD@{11}: checkout: moving from master to 5159b0255aec18330384a1a115058560a6a4574a
5159b02 HEAD@{12}: commit: fixup! Fixed compilation issue.
20f23cb HEAD@{13}: commit: fixup! Merged with stash.
8916f61 HEAD@{14}: commit: fixup! Some fixes.
$:~/src/dsp_prod$ git reset --hard HEAD@{12}
HEAD is now at 5159b02 fixup! Fixed compilation issue.

Yeah, I hadn't quite figured out how to use fixup at that point either. Resetting to ORIG_HEAD didn't help, but using a combination of reflog & reset --hard did the trick and my commit history was back to what it was before my mistake.

Btw, if you want to see the reflogs but want a nicer output (which lacks some of the stuff) you can also say "git log -g".

No comments:

Post a Comment