Undoing An Erroneous Force Push: The Git Reflog
Recently, I’ve been working on a project maintained by multiple developers. There are a decent number of pull requests hanging around, and a lot of work happening at all times. One of my roles has been to prepare open pull requests for a clean and easy merge.
Not too tricky of a task, most of the time. Rebase the branch onto master, resolve all the conflicts, push up the changes, and it’s good to go.
Unless you mess up the rebase, incorrectly resolving merge conflicts. Running the tests will save you most of the time, but not always. And if you get too far, you may have just force pushed a broken branch onto the PR. Oops.
Luckily, this sort of error is not the end of the world. Not at all.
There are number of ways to deal with inadvertent force pushes; I’d recommend this blog post if you haven’t come across it already. But in this case, a very simple solution is to take advantage of the reflog
.
If you’re in your project’s directory and you type git reflog
on the command line, you’ll see a history of actions you’ve taken within the project: moving between branches, making commits, etc.
If you pinpoint the SHA corresponding to the place where everything went awry (e.g. the place right before where you began the faulty rebase), you can git checkout
that SHA and begin again. Or just breathe a sigh of relief as you push it up un-catastrophically-rebased.