Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Deleted a branch from Git

Hi , I have accidentally deleted a branch from my Git repository.

How to get it back or recover it?

 

1 answer

1 accepted

6 votes
Answer accepted

Hi @Tarak Mehta ,

Welcome to the community!

Make sure to perform all of this locally, and confirm your repo is in the state you desire before pushing to Bitbucket Cloud. It may also be a good idea to clone your current repo, and test these solutions out first.

 

 If you just deleted the branch, you'll see something like this in your terminal:

Deleted branch <your-branch> (was <sha>)

To restore the branch, use: 

git checkout -b <branch> <sha>

If you don't know the 'sha' off the top of your head, you can:

Find the 'sha' for the commit at the tip of your deleted branch using: 

git reflog

To restore the branch, use: 

git checkout -b <branch> <sha>

If your commits are not in your reflog:

You can try recovering a branch by reseting your branch to the sha of the commit found using a command like: 

git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\  -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt

 

You can then display each commit using one of these: 

git log -p <commit>

git cat-file -p <commit>

Thanks for your quick reply!

Like Soumyadeep Mandal likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,927 views 10 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you