Github’s reflog

Or how to recover lost commits that were not available in local

Git’s reflog is a very powerful tool and can be used to correct most mistakes. But what about the scenario where you inadvertently force pushed a local branch without fetching your coworkers changes first? One option of course, is to recover those commits from the coworkers local branch but if you are using Github there’s a way out.

First, find the commit id before you pushed your changes by using the Events API.

curl -u <username>

This will return a JSON blob of the most recent events in the repo — pretty much like the reflog command. You would have to sift through the blob to locate your commit(s) that was lost. You can these use the ref/sha to create a new branch.

Create a new branch for the ref using the Create Reference API:

curl -u <github-username> -X POST -d ‘{“ref”:”refs/heads/<new-branch-name>”, “sha”:”<sha-from-step-1>"}’