How to recover a deleted branch from git?

Question:

I just deleted a git branch.
For example :

mkdir test
cd test
git init
echo "Viva yo" > test.txt
git add .
git commit -m "test"
git checkout -b rama
echo "Viva tu" > test2.txt
git add .
git commit -m "test2"
git checkout master
git branch -D rama

How can I recover the branch that I just deleted?
If you knew the hash of the commit you could retrieve it with:

git checkout -b rama EL_HASH

But I don't know what hash it has, I didn't look at it.

Answer:

You can try:

git fsck --full --no-reflogs | grep commit

To find the commit HEAD of the deleted branch.

If you want to find which commit is correct you can use git show

And once you have the commit message located, create the branch again with a git branch <uid>

Scroll to Top