Home > Net >  create a branch on the 'lost' commit of a force pushed branch
create a branch on the 'lost' commit of a force pushed branch

Time:11-22

I have a github repo that got an accidental force push on a branch.

The PR of this branch shows both the new and the old commit and i even can browse the files of this commit - so everything is still be there.

Can i somehow add a branchname to this commit, so that it can get checked out normally (and at the end correct the messed up initial branch)?

CodePudding user response:

If you have the complete sha of said commit, you can run :

git fetch origin <sha>
git switch -c newbranch <sha>

as commented by @kadewu : the sha in git fetch origin sha needs to be the complete sha (40 chars), otherwise git will try to interpret it as a reference name.


Also worth noting : if you (or anyone else) had the correct state of the remote branch at some point (eg: after a git fetch, or a git pull on any branch), you can find that correct commit by looking at :

git reflog origin/thatbranch

CodePudding user response:

Create a branch using GitHub's API, then you can fetch and checkout that branch.

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"ref":"refs/heads/<new-branch-name>", "sha":"<sha>"}' https://api.github.com/repos/<namespace>/<repo>/git/refs

https://objectpartners.com/2014/02/11/recovering-a-commit-from-githubs-reflog/

  • Related