Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover deleted branch in Git [duplicate]

Tags:

git

git-branch

I have deleted my branch by mistake like this:

git branch -D demo 

But I want to recover it… I get this after git reflog

541b2f5 HEAD@{23}: checkout: moving from demo to master 06fa6d5 HEAD@{24}: commit (merge): remove ajax call for deleting variables and transfomers b84b60a HEAD@{25}: checkout: moving from demo1 to demo 

I want to create branch with sha 06fa6d5… so I tried this:

git checkout -b demo  06fa6d5  git checkout -b demo  HEAD@{24} 

But I didn't get code from that…

like image 442
Nalini Wanjale Avatar asked May 28 '13 13:05

Nalini Wanjale


People also ask

Can I recover deleted branch in git?

A deleted Git branch can be restored at any time, regardless of when it was deleted. Open your repo on the web and select the Branches view. Search for the exact branch name using the Search all branches box in the upper right. Click the link to Search for exact match in deleted branches.

How do I restore a branch?

Yes, you should be able to do git reflog --no-abbrev and find the SHA1 for the commit at the tip of your deleted branch, then just git checkout [sha] . And once you're at that commit, you can just git checkout -b [branchname] to recreate the branch from there.

Does Github save deleted branches?

Yes, it's possible to restore a deleted branch from git.


2 Answers

Create a list of all dangling or unreachable commits.

git fsck --full --no-reflogs --unreachable --lost-found 

Print a list of commit messages for all commits in the lost and found.

ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline 

Find your missing commit through the process of manual inspection (i.e. reading). Create a new branch with the missing commit as the branch head.

git checkout -b branch-name SHA 
like image 186
vitthal-gaikwad Avatar answered Oct 11 '22 10:10

vitthal-gaikwad


Having got the potential sha1 for the last tip of branch demo, use gitk sha1 to actually browse the commit's history to check you have the right one.

like image 26
Philip Oakley Avatar answered Oct 11 '22 11:10

Philip Oakley