Github’s reflog

Sankara Rameswaran
Dec 8, 2014 · 1 min read

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> https://api.github.com/repos/:owner/:repo/events

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>"}’ https://api.github.com/repos/:owner/:repo/git/refs

Git Tips

Tips about git

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store