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?
From my understanding if the branch to be deleted can be reached by another branch, you can delete it safely using
git branch -d [branch]
and your work is not lost. Remember that a branch is not a snapshot, but a pointer to one. So when you delete a branch you delete a pointer.
You won't even lose work if you delete a branch which cannot be reached by another one. Of course it won't be as easy as checking out the commit hash, but you can still do it. That's why Git is unable to delete a branch which cannot be reached by using -d. Instead you have to use
git branch -D [branch]
This is part of a must watch video from Scott Chacon about Git. Check minute 58:00 when he talks about branches and how delete them.
Introduction to Git with Scott Chacon of GitHub