How to recover an erased branch of git?


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 commit hash, you could recover it with:

git checkout -b rama EL_HASH

But I do not know what the hash is, I did not look at it.

asked by Jose Antonio Dura Olmos 20.09.2017 в 11:24

2 answers


You can try:

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

To find the HEAD commit of the deleted branch.

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

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

answered by 20.09.2017 / 12:40

As I understand you can use git reflog to see the commits that you have been doing and you have to appear the SHA.

With that you can do git checkout -b <rama> <sha>

answered by 20.09.2017 в 11:48