Missed Team ’24? Catch up on announcements here.

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

Deleted a branch from Git

Tarak Mehta
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 26, 2020

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
Soumyadeep Mandal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 26, 2020

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>

Tarak Mehta
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 26, 2020

Thanks for your quick reply!

Like Soumyadeep Mandal likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events