Skip to content

Instantly share code, notes, and snippets.

@alfredo
Last active December 8, 2017 14:34
Show Gist options
  • Save alfredo/4c88b976d885c7d0831c1c5354e45076 to your computer and use it in GitHub Desktop.
Save alfredo/4c88b976d885c7d0831c1c5354e45076 to your computer and use it in GitHub Desktop.
Github reflog

Reflog-ish for Github (to recover a lost commit)

The following steps can be taken to recover a lost Github commit. e.g. An accidental push force removed the commits.

  1. Get a personal token with repo access from: https://github.com/settings/tokens Make sure the token has privileges to read/write the repository.

  2. Use the token to access the events history of the repo: The relevant key value in the JSON response is the sha:

$ curl -u {USERNAME}:{ACCESS_TOKEN} 'https://api.github.com/repos/{OWNER}/{REPOSITORY}/events?page=1'

Please note that the events list can be paginated and the variables in the previous curl command must be updated to match your repository.

  1. Find the sha value of the commit must be restored.

  2. Create a new branch using the commit sha value.

curl -u {USERNAME}:{ACCESS_TOKEN} -X POST -d '{"ref":"refs/heads/{NEW_BRANCH}", "sha":"{SHA_VALUE}"}' https://api.github.com/repos/{OWNER}/{REPOSITORY}/git/refs
  1. The new branch will be available in Github and that can be used to recover any lost commits.

  2. ...

  3. Profit???

Adventure time Jake and Fin high fiving

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment