Skip to content

Instantly share code, notes, and snippets.

@inancgumus
Last active October 20, 2020 22:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inancgumus/71a72f472343f38b27b4e53ea4d00acb to your computer and use it in GitHub Desktop.
Save inancgumus/71a72f472343f38b27b4e53ea4d00acb to your computer and use it in GitHub Desktop.
Restore github commit

If you've made a wrong forceful push or just want to recover from old commits in github, then you can use this command flow:

# find the commit, just like git reflog
curl -u <username> https://api.github.com/repos/:owner/:repo/events

# create a new branch from the commit sha found in previous step
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
# replace the master branch (if you want -- be careful)
git pull --all
git checkout <new-branch-name>
git branch -M master
git push --force
git push :<new-branch-name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment