Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover deleted remote branch in Git [duplicate]

If I run git branch -d XYZ, is there a way to recover the branch? Is there a way to go back as if I didn't run the delete branch command?

like image 522
prosseek Avatar asked Nov 21 '22 08:11

prosseek


1 Answers

Yes, you should be able to do git reflog --no-abbrev and find the SHA1 for the commit at the tip of your deleted branch, then just git checkout [sha]. And once you're at that commit, you can just git checkout -b [branchname] to recreate the branch from there.


Credit to @Cascabel for this condensed/one-liner version and @Snowcrash for how to obtain the sha.

If you've just deleted the branch you'll see something like this in your terminal Deleted branch <your-branch> (was <sha>). Then just use that <sha> in this one-liner:

git checkout -b <your-branch> <sha>
like image 176
tfe Avatar answered Dec 09 '22 08:12

tfe