How to undo a mistaken git rebase (LIFE SAVER)

Suppose you perform a git rebase master on your local branch and didn't realize it wasn't what you wanted until after you force pushed it to the remote.

What to do now? Is there a way to restore your branch to where it was before that rebase?

Yes there is.

GIT REFLOG!!

Official documentation : https://git-scm.com/docs/git-reflog

More specifically, use the reflog for the branch

For instance if youhad just rebased testBranch, do:

$ git reflog testBranch
73d836b testBranch@{0}: rebase finished: refs/heads/testBranch onto e806e41f1fe22624e6546abd65c332c934214891
129e6d3 testBranch@{1}: commit: some sort of commit message

If you then do,

$ git checkout testBranch # if needed
$ git reset — hard 129e6d3 # or testBranch@{1}

and you should have the original back