Skip to content

Instantly share code, notes, and snippets.

@agarwalparas
Created November 3, 2017 04:06
Show Gist options
  • Save agarwalparas/d355a950148702cc7ba82abc4d1943bf to your computer and use it in GitHub Desktop.
Save agarwalparas/d355a950148702cc7ba82abc4d1943bf to your computer and use it in GitHub Desktop.
Recover Force Push on Github
# First, you must get the previous commit sha, the one before the forced push:
## Hit through terminal
curl -u <username> https://api.github.com/repos/:owner/:repo/events
# Then you can create a branch from this sha:
## Hit through terminal
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
#Finally, you can clone the repository locally, and force push again to master:
git clone repo@github
git checkout master
git reset --hard origin/<new-branch-name>
git push -f origin master
Credit: Sankara Rameswaran
@Freed-Wu
Copy link

Freed-Wu commented Aug 12, 2022

After the incorrect operation, I try to recover my git record.

git clone --depth=1 username/repo
vi XXX
git commit --amend --no-edit
git push -f

However, I always met:

{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/reference/git#create-a-reference"
}

I solve it. Refer https://docs.github.com/en/rest/git/refs#create-a-reference

@boris1993
Copy link

Thank you! You saved my life!

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