Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to recover a git branch you accidentally deleted

UPDATE: A better way! (August 2015)

As pointed out by @johntyree in the comments, using git reflog is easier and more reliable. Thanks for the suggestion!

 $ git reflog
1ed7510 HEAD@{1}: checkout: moving from develop to 1ed7510
3970d09 HEAD@{2}: checkout: moving from b-fix-build to develop
1ed7510 HEAD@{3}: commit: got everything working the way I want
70b3696 HEAD@{4}: commit: upgrade rails, do some refactoring
98f2fc2 HEAD@{5}: commit: a couple code cleanups
d09f35e HEAD@{6}: commit: remove test method - it served it's purpose and now it must go
d586a93 HEAD@{10}: commit: aha! that is why I'm so fail
4644046 HEAD@{11}: commit: cleaning up the initial migration for dev/test environments
323df37 HEAD@{15}: commit: bump ruby version
eab861c HEAD@{16}: commit: bundle update EVERYTHING
2b544c4 HEAD@{17}: commit: fixing what few tests actually exist - a.k.a., wow! does this app even work?
3970d09 HEAD@{18}: checkout: moving from develop to b-fix-build
3970d09 HEAD@{19}: pull: Fast-forward

Once you find the commit you're looking for, create a new branch from it and you're done!

$ git checkout -b branch-name 70b3696
Switched to a new branch 'branch-name'

Original post

Oops! I accidentally deleted a local git branch, and I haven't pushed it to a remote server yet. The branch has several important commits, and it hasn't been merged with any other branches yet. How do I find the missing branch?

1. Create a list of all dangling or unreachable commits.

$ git fsck --full --no-reflogs --unreachable --lost-found
unreachable tree 4a407b1b09e0d8a16be70aa1547332432a698e18
unreachable tree 5040d8cf08c78119e66b9a3f8c4b61a240229259
unreachable tree 60c0ce61b040f5e604850f747f525e88043dae12
unreachable tree f080522d06b9853a2f18eeeb898724da4af7aed9
unreachable blob bf01f514add2ada00a7ae5c666493d30d639018c
...

These commits are copied into .git/lost-found/commit/, and non-commit objects are copied into .git/lost-found/other/.

2. 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
63b65d3784b16f92bb370ad6a2c1091a05824ecc Call #to_s on value before calling some string methods, like gsub
6ed99e63db69ca04f0cc78081a1fd471289551b2 On master: search and reset page
973d9be3e2cefcd0c5801ad9cd1b2e18774b4bee Rename decorator proxy to decorator context
9ae38fc6b0548cab08ccee1178db0ba0edeafdb2 foo
9e994ca0c0c4785ab45bf64b367fdacccc4575a9 foo [#12345]
9efa6b28b3b0a89c312484f28cf589385d613dfd On master: mysql db config
c57a67c7e1c21fa0c32f152e73d8c3376cad19a0 bar
cb3d67e1aa2226ab9d816fc541f36ff698bfda41 WIP on master: 40a4453 Use #website_url instead of #template_url or #url
def0a251bd29b7fc54a5622e364711f60097b826 Example tabs for export show page (no styles)w

3. Find your missing commit through the process of manual inspection (i.e. reading).

If you need more information on a commit, you can always use a more detailed log command, such as git log -p --stat --color 9ae38fc.

4. Create a new branch with the missing commit as the branch head.

$ git checkout -b branch-name 9ae38fc
Switched to a new branch 'branch-name'
@trevorwhealy
Copy link

trevorwhealy commented Jul 25, 2017

Thank you so much.

@mbenalirichard
Copy link

mbenalirichard commented Jul 31, 2017

Thank you so much !

As said previously I used :

git fsck --lost-found       
ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline
git checkout <tag>

@damusix
Copy link

damusix commented Aug 4, 2017

Dude you friggin saved my life... where the hell can I buy you a beer?!

@mberrueta
Copy link

mberrueta commented Sep 21, 2017

thanks man! awesome!

@lukeocodes
Copy link

lukeocodes commented Nov 3, 2017

Very helpful... 👍

@ilterocal
Copy link

ilterocal commented Jan 8, 2018

Thanks a lot! I was about to jump out of my window at 12th floor, after deleting a branch with two weeks of hard work in it, without merging it to our master.

@waqashamid
Copy link

waqashamid commented Apr 14, 2018

I have neither the local branch nor the remote. I deleted it manually from my Github repo. I have nothing on my local machine. What do I have to do to restore my remote branch?

@CheragV
Copy link

CheragV commented May 2, 2018

Thanks a lot !! learned something new here 👍

@sawzeeyy
Copy link

sawzeeyy commented Aug 9, 2018

Thanks a lot! This was helpful for me today

Thanks again.

@surjit-logiciel
Copy link

surjit-logiciel commented Sep 5, 2018

Thanks a lot! Today, it's really helpful for me and saves my 3days work 👍
Thanks a lot again. 🥇

@VishwasShashidhar
Copy link

VishwasShashidhar commented Feb 8, 2019

This is awesome!

@FlipgridMartin
Copy link

FlipgridMartin commented Feb 15, 2019

Savior 🙏

@MitchellBaker65
Copy link

MitchellBaker65 commented Sep 26, 2019

This guy is still saving lives in 2019

@18549
Copy link

18549 commented Nov 27, 2019

Doesn't this only work for a branch that's been deleted but which hasn't yet had the dangling commit garbage collected? In which case shouldn't the title be 'How to recover a git branch you recently accidentally deleted'.

As far as I'm aware there is no way to recover the commits of an unmerged deleted branch that's been garbage collected, making Git the only SCS I know of that you can commit work to and then subsequently lose, but I'd be happy to be proven wrong.

@MatheusMuriel
Copy link

MatheusMuriel commented Jan 9, 2020

Niiice, thanks

@erickterra
Copy link

erickterra commented Jan 23, 2020

You saved my life. Thanks

@Rudo2204
Copy link

Rudo2204 commented Feb 18, 2020

Thanks you, you saved me a full day of work here!

@ngocgiang99
Copy link

ngocgiang99 commented Dec 26, 2020

You save my life now.

@NathanTarbert
Copy link

NathanTarbert commented Jan 11, 2021

So I created a repo, scrapped it, tried to recreate a repo it said it was already initialized. My code was complete in a branch called branch2-2 somehow modified it and lost a folder in the branch. Is there a way to retrieve it? Technically the branch only existed on my local machine and it deleted the folder right out of my VS code... Not sure what to do. Thanks!

@fxyw
Copy link

fxyw commented Jan 24, 2021

@raflymln
Copy link

raflymln commented Mar 8, 2021

You saving me from my work for 3 month non-stop, thank you so much man, i hope you're doing great 👍

@francescopappalardo
Copy link

francescopappalardo commented Apr 22, 2021

Thank you very much also from my side. It saved two working days!

@sgacommentor
Copy link

sgacommentor commented Apr 26, 2021

Thank you! Saved me many hours of (re)work!

@lbluitt
Copy link

lbluitt commented May 17, 2021

Thank you!!!! too useful

@GoudekettingRM
Copy link

GoudekettingRM commented Jun 25, 2021

Saved my butt here, thank you!

@akenz1901
Copy link

akenz1901 commented Jan 18, 2022

A huge Thank you to everyone that contributed to this solution

@sanketbhangale43
Copy link

sanketbhangale43 commented Jun 4, 2022

Thank you

@sanjay-sridharan
Copy link

sanjay-sridharan commented Nov 3, 2022

Thank you, Mr Jordan Bach!

@renat-fayzullin
Copy link

renat-fayzullin commented Nov 10, 2022

Thanks!

@egemenertugrul
Copy link

egemenertugrul commented Nov 16, 2022

Blessings upon thee. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment