Can I recover a branch after its deletion in Git?

prosseek picture prosseek · Sep 4, 2010 · Viewed 366.1k times · Source

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?

Answer

tfe picture tfe · Sep 4, 2010

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.

You can do it in one step:

git checkout -b <branch> <sha>