The following steps can be taken to recover a lost Github commit. e.g. An accidental push force removed the commits.
-
Get a personal token with repo access from: https://github.com/settings/tokens Make sure the token has privileges to read/write the repository.
-
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.
-
Find the
sha
value of the commit must be restored. -
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
-
The new branch will be available in Github and that can be used to recover any lost commits.
-
...
-
Profit???