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?
First go to git batch the move to your project like :
cd android studio project
cd Myproject
then type :
git reflog
You all have a list of the changes and the reference number take the ref number then checkout
from android studio or from the git betcha.
another solution take the ref number and go to android studio click on git branches down then click on checkout tag or revision past the reference number then lol you have the branches.
Adding to tfe answer: there is also the git-resurrect.sh script in the contrib/
area of the Git sources (in git.git repository), which might help you.
git-resurrect <name>
attempts to find traces of a branch tip called<name>
, and tries to resurrect it. Currently, the reflog is searched for checkout messages, and with-r
also merge messages. With-m
and-t
, the history of all refs is scanned forMerge <name> into other
/Merge <other> into <name>
(respectively) commit subjects, which is rather slow but allows you to resurrect other people's topic branches.
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.
Deleted branch <your-branch> (was <sha>)
2.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:
git reflog
git checkout -b <branch> <sha>
If your commits are not in your reflog:
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
2.You can then display each commit using one of these:
git log -p <commit>
git cat-file -p <commit>
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
Adding to tfe's answer, you can recover with this process mentioned, unless it's commits are not garbage collected. Git branch is simply a pointer to a particular commit in the commit tree. But if you delete the pointer, and the commits on that branch are not merged into other existing branch, then git treats it as dangling commits and removes them during garbage collection, which it may run automatically periodically.
If your branch wasn't merged to an existing branch, and if it was garbage collected, then you will loose all commits up until the point from where branch was forked from an existing branch.
If you like to use a GUI, you can perform the entire operation with gitk.
gitk --reflog
This will allow you to see the branch's commit history as if the branch hadn't been deleted. Now simply right click on the most recent commit to the branch and select the menu option Create new branch
.