16 de octubre de 2014

How to undo a git rebase / squash / fixup? (a.k.a. "we all poop it sometimes")



Today I had a bit of a mess in one of my PRs by doing an interactive rebase (git rebase -i HEAD~) and a fixup / squash.

Now: What to do now, after doing the wrong squash?

Good news: virtually everything can be undone in GIT :)

"git reflog" was what I was looking for to put thing where they were before:

$ git reflog

At this point you'll see a long list of commits like this one:

 $ git reflog
e074e05 HEAD@{0}: rebase: aborting
b3885fe HEAD@{1}: rebase -i (start): checkout HEAD~7
e074e05 HEAD@{2}: pull origin master: Merge made by the 'recursive' strategy.
b3885fe HEAD@{3}: rebase -i (finish): returning to refs/heads/ticket
b3885fe HEAD@{4}: rebase -i (pick): something
....

Then you need to pick the action you want to go back to and reset to it with this command:

$ git reset --hard HEAD@{N}

Where "N" is the number between curly braces from the list of commits displayed by git reflog.

Et voilà!

No hay comentarios: