Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover deleted unmerged branch

Tags:

git

I found some solutions on how to recover git branches by using

  1. git reflog
  2. git fsck

I can't reach some commits from deleted branch or any history of deletion of this branch.

The local branch is not yet merged in my master branch and accidentally deleted. Is it possible to recover it? Thanks for your help!

like image 824
Glenn Avatar asked Jun 10 '14 03:06

Glenn


1 Answers

You could start with:

git reflog | grep 'to branchname'

This will match a reflog line like the following:

219daf7 HEAD@{20}: checkout: moving from master to branchname

You can use git checkout 219daf7 to have a look around and make sure it's the right commit, or I recommend tig for browsing your repository and you can use tig 219daf7 to see the history from that commit. (Bear in mind that git checkout will add entries to your reflog, whereas tig won't.)

Once you're satisfied you've found the right commit, you can use the following to create the branch again:

git checkout -b branchname 219daf7

Failing that, you can use the fsck approach:

git fsck --full --no-reflogs --unreachable --lost-found | grep commit

This will list any deleted commits:

unreachable commit 219daf70a24e635cd95c1493c341585bbf64a61d

You can then use the commit ID in the same way as above.

like image 139
cmbuckley Avatar answered Sep 19 '22 14:09

cmbuckley