Create a gist now

Instantly share code, notes, and snippets.

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'
@sly7-7

This comment has been minimized.

Show comment Hide comment
@sly7-7

sly7-7 Dec 19, 2013

Thanks a lot for this. This saves me one day and a half of work :)

sly7-7 commented Dec 19, 2013

Thanks a lot for this. This saves me one day and a half of work :)

@francescostella

This comment has been minimized.

Show comment Hide comment
@francescostella

francescostella Mar 28, 2014

Great! Thank you very much for the article! It worked nicely.
I delete a local branch for mistake and I recovered 3 days of work!

Note: the commad
$ git fsck --full --no-reflogs --unreachable --lost-found

does not create the folder
.git/lost-found/commit/

I had to skip to the last step and run
$ git checkout -b branch-name COMMIT-NAME

Great! Thank you very much for the article! It worked nicely.
I delete a local branch for mistake and I recovered 3 days of work!

Note: the commad
$ git fsck --full --no-reflogs --unreachable --lost-found

does not create the folder
.git/lost-found/commit/

I had to skip to the last step and run
$ git checkout -b branch-name COMMIT-NAME

@andreorvalho

This comment has been minimized.

Show comment Hide comment
@andreorvalho

andreorvalho Apr 10, 2014

I get ls: .git/lost-found/commit/: No such file or directory when I try to run the second step, I am not sure where I should look for it!

I get ls: .git/lost-found/commit/: No such file or directory when I try to run the second step, I am not sure where I should look for it!

@marlocorridor

This comment has been minimized.

Show comment Hide comment
@marlocorridor

marlocorridor May 12, 2014

I tried this on windows OS and it's not working.

sorry, I just did'nt understand the commands.

so this is the actual recovering command:
git checkout -b <branch> <sha>

I tried this on windows OS and it's not working.

sorry, I just did'nt understand the commands.

so this is the actual recovering command:
git checkout -b <branch> <sha>

@cff2880

This comment has been minimized.

Show comment Hide comment
@cff2880

cff2880 Jul 9, 2014

Use this following command
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
then, you can see the commits file inside .git folder under the name lost-found.txt

cff2880 commented Jul 9, 2014

Use this following command
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
then, you can see the commits file inside .git folder under the name lost-found.txt

@raul000

This comment has been minimized.

Show comment Hide comment
@raul000

raul000 Aug 5, 2014

Note: the commad
$ git fsck --full --no-reflogs --unreachable --lost-found

does not create the folder
.git/lost-found/commit/

use
git fsck --lost-found

raul000 commented Aug 5, 2014

Note: the commad
$ git fsck --full --no-reflogs --unreachable --lost-found

does not create the folder
.git/lost-found/commit/

use
git fsck --lost-found

@Efreeto

This comment has been minimized.

Show comment Hide comment
@Efreeto

Efreeto Sep 28, 2014

Thank you jbgo!

For anyone stuck with dangling blobs like me, you can simply rename the files in .git/lost-found/other directory. Original file names are lost so you might have to dig through with some text search tool.

Efreeto commented Sep 28, 2014

Thank you jbgo!

For anyone stuck with dangling blobs like me, you can simply rename the files in .git/lost-found/other directory. Original file names are lost so you might have to dig through with some text search tool.

@esttorhe

This comment has been minimized.

Show comment Hide comment
@esttorhe

esttorhe Apr 21, 2015

THANK YOU @jbgo!!!!

Saved my whole day with this command πŸ˜„

THANK YOU @jbgo!!!!

Saved my whole day with this command πŸ˜„

@huckl3b3rry87

This comment has been minimized.

Show comment Hide comment
@huckl3b3rry87

huckl3b3rry87 Apr 22, 2015

There are no commit messages in my lost and found, but there are unreachable commits, when i type:
$ git fsck --full --no-reflogs --unreachable --lost-found I get:
Checking object directories: 100% (256/256), done.
unreachable commit 78d0659cd8bf471dc6d94d5d80691e21979e5fde
unreachable commit 8970d0a30ab42e45eb10e474b0fd76f082a35008
unreachable commit 99f040af9dabbb0d4afcc2a133158835cdf0fe1c
unreachable blob b910a0caf457cccce18d603acc0256a639668fcc
unreachable blob dd58ec3e67e16d1a4ee99c4df5a7f479029c6000
unreachable commit e6b8f90ff9ef19a52ad06a288894c5c254b9d0bb
unreachable blob 35390ef7dc2372f74028f02a419d4e725a03ef2d

Then when I type:
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

I get an empty text document. Basically what happened was,I accidentally synced my folder before I committed new changes. I tried to commit changes and I thought that it had finished and the I clicked sync to get data from the internet, but it failed to commit and it just synced and I lost a bunch of work. Then I removed the repository and added it again ( I know a mistake).

Please help, I am new to this and lost a tremendous amount of work. Thanks!

There are no commit messages in my lost and found, but there are unreachable commits, when i type:
$ git fsck --full --no-reflogs --unreachable --lost-found I get:
Checking object directories: 100% (256/256), done.
unreachable commit 78d0659cd8bf471dc6d94d5d80691e21979e5fde
unreachable commit 8970d0a30ab42e45eb10e474b0fd76f082a35008
unreachable commit 99f040af9dabbb0d4afcc2a133158835cdf0fe1c
unreachable blob b910a0caf457cccce18d603acc0256a639668fcc
unreachable blob dd58ec3e67e16d1a4ee99c4df5a7f479029c6000
unreachable commit e6b8f90ff9ef19a52ad06a288894c5c254b9d0bb
unreachable blob 35390ef7dc2372f74028f02a419d4e725a03ef2d

Then when I type:
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

I get an empty text document. Basically what happened was,I accidentally synced my folder before I committed new changes. I tried to commit changes and I thought that it had finished and the I clicked sync to get data from the internet, but it failed to commit and it just synced and I lost a bunch of work. Then I removed the repository and added it again ( I know a mistake).

Please help, I am new to this and lost a tremendous amount of work. Thanks!

@johntyree

This comment has been minimized.

Show comment Hide comment
@johntyree

johntyree May 15, 2015

git reflog

git reflog

@mateusrevoredo

This comment has been minimized.

Show comment Hide comment
@mateusrevoredo

mateusrevoredo May 19, 2015

WOW! @jbgo

Thank you so much!

You just saved me more than a day of work accidentally deleted.

WOW! @jbgo

Thank you so much!

You just saved me more than a day of work accidentally deleted.

@Sporky023

This comment has been minimized.

Show comment Hide comment
@Sporky023

Sporky023 May 27, 2015

For those who are having the problem of the /lost-found/ directory not being created, you can use the following to view all of the lost commit messages:

git fsck --lost-found | grep commit | cut -d ' ' -f 3 | xargs -n 1 git log -n 1 --pretty=oneline

Basically it just slices out the blobs, picks the third word of that output which is the SHA then passes it to the command in step 2

For those who are having the problem of the /lost-found/ directory not being created, you can use the following to view all of the lost commit messages:

git fsck --lost-found | grep commit | cut -d ' ' -f 3 | xargs -n 1 git log -n 1 --pretty=oneline

Basically it just slices out the blobs, picks the third word of that output which is the SHA then passes it to the command in step 2

@SajilKoroth

This comment has been minimized.

Show comment Hide comment
@SajilKoroth

SajilKoroth Jun 27, 2015

Saved me a week

Saved me a week

@firvorski

This comment has been minimized.

Show comment Hide comment
@firvorski

firvorski Jul 26, 2015

Thank you very much!
This saved me a lot of time.

Thank you very much!
This saved me a lot of time.

@NadunW

This comment has been minimized.

Show comment Hide comment
@NadunW

NadunW Jul 30, 2015

Thanks a lot..

You saved my 20 days work...

NadunW commented Jul 30, 2015

Thanks a lot..

You saved my 20 days work...

@kushal1211

This comment has been minimized.

Show comment Hide comment
@kushal1211

kushal1211 Aug 11, 2015

thanks... you saved a life...

thanks... you saved a life...

@TatyCat

This comment has been minimized.

Show comment Hide comment
@TatyCat

TatyCat Sep 29, 2015

Thank you so much for including an example of what the command is supposed to look like!

TatyCat commented Sep 29, 2015

Thank you so much for including an example of what the command is supposed to look like!

@icyflame

This comment has been minimized.

Show comment Hide comment
@icyflame

icyflame Oct 2, 2015

@cff2880 Thanks a lot! Your command works!

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

icyflame commented Oct 2, 2015

@cff2880 Thanks a lot! Your command works!

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
@renanborgez

This comment has been minimized.

Show comment Hide comment
@renanborgez

renanborgez Nov 24, 2015

Thanks!!

Thanks!!

@reyarias

This comment has been minimized.

Show comment Hide comment
@reyarias

reyarias Dec 13, 2015

I hit sync and it deleted a project folder I had been working on for a week, I have to turn it in today! :/ Any way I can retrieve it?

I hit sync and it deleted a project folder I had been working on for a week, I have to turn it in today! :/ Any way I can retrieve it?

@rylectro

This comment has been minimized.

Show comment Hide comment
@rylectro

rylectro Dec 17, 2015

Thank you for saving my sanity.

Thank you for saving my sanity.

@bosnian

This comment has been minimized.

Show comment Hide comment
@bosnian

bosnian Feb 24, 2016

πŸ‘

bosnian commented Feb 24, 2016

πŸ‘

@CatherineMcMahon

This comment has been minimized.

Show comment Hide comment
@CatherineMcMahon

CatherineMcMahon Apr 29, 2016

Yo I also hit sync and lost all my local unpushed files/was left with my last remote save. What do I do??

Yo I also hit sync and lost all my local unpushed files/was left with my last remote save. What do I do??

@jomonkj

This comment has been minimized.

Show comment Hide comment
@jomonkj

jomonkj Jun 14, 2016

Thanks a lot....

jomonkj commented Jun 14, 2016

Thanks a lot....

@remyamurali

This comment has been minimized.

Show comment Hide comment
@remyamurali

remyamurali Jun 14, 2016

Thanks a lot, It works for me....

Thanks a lot, It works for me....

@aiosk

This comment has been minimized.

Show comment Hide comment
@aiosk

aiosk Aug 4, 2016

thanks a lot,
it saved my 2 week worth of work

aiosk commented Aug 4, 2016

thanks a lot,
it saved my 2 week worth of work

@bodedra

This comment has been minimized.

Show comment Hide comment
@bodedra

bodedra Sep 13, 2016

Thanks a lot for useful post.

bodedra commented Sep 13, 2016

Thanks a lot for useful post.

@PombaM

This comment has been minimized.

Show comment Hide comment
@PombaM

PombaM Nov 10, 2016

Thanks man, you saved my life long live git reflog! Now where is the damn πŸ‘ button?

PombaM commented Nov 10, 2016

Thanks man, you saved my life long live git reflog! Now where is the damn πŸ‘ button?

@DomDomHaas

This comment has been minimized.

Show comment Hide comment
@DomDomHaas

DomDomHaas Nov 11, 2016

That just saaaaaaved my life! πŸ˜„ Or one week experimenting and one week of work.
❀️

That just saaaaaaved my life! πŸ˜„ Or one week experimenting and one week of work.
❀️

@williambeeler

This comment has been minimized.

Show comment Hide comment
@williambeeler

williambeeler Nov 23, 2016

Dude, you just saved my rear end.. Big time! What I did, which could be a little different from what others did, was once I had the list of blobs and unreachable commits, I just used the cat file command with git to get the blob contents and restore the work that I had lost. It wasn't more than a couple of hours of work across a few files, so it was a lot easier than some of the other commenters I'm seeing here.

git cat-file -p 52fbee04a2...blahblahblah

Thanks a ton!!

Dude, you just saved my rear end.. Big time! What I did, which could be a little different from what others did, was once I had the list of blobs and unreachable commits, I just used the cat file command with git to get the blob contents and restore the work that I had lost. It wasn't more than a couple of hours of work across a few files, so it was a lot easier than some of the other commenters I'm seeing here.

git cat-file -p 52fbee04a2...blahblahblah

Thanks a ton!!

@walkingbrad

This comment has been minimized.

Show comment Hide comment
@walkingbrad

walkingbrad Jan 5, 2017

OMGGGGGGGG ❀️

OMGGGGGGGG ❀️

@zapkub

This comment has been minimized.

Show comment Hide comment
@zapkub

zapkub Jan 12, 2017

MAN, you save my entire week !!

zapkub commented Jan 12, 2017

MAN, you save my entire week !!

@SKHameed86

This comment has been minimized.

Show comment Hide comment
@SKHameed86

SKHameed86 Feb 15, 2017

Thank you! Realised after I had deleted my branch that it was the only copy of the code I'd been working on!

Total lifesaver πŸ‘

Thank you! Realised after I had deleted my branch that it was the only copy of the code I'd been working on!

Total lifesaver πŸ‘

@amanhigh

This comment has been minimized.

Show comment Hide comment
@amanhigh

amanhigh Mar 16, 2017

Thanks a lot it really helped :)

Thanks a lot it really helped :)

@sreenathveer

This comment has been minimized.

Show comment Hide comment
@sreenathveer

sreenathveer Mar 20, 2017

I have lot (19k) of non-commit files in my .git/lost-found/other folder with sha1. How can i recover them. I did some research i came to know that, git show sha1 will show the file content, i cant do all these manually. How can i get back all my files. Is there any way, please let me know if some one might come across same problem.

I have lot (19k) of non-commit files in my .git/lost-found/other folder with sha1. How can i recover them. I did some research i came to know that, git show sha1 will show the file content, i cant do all these manually. How can i get back all my files. Is there any way, please let me know if some one might come across same problem.

@PikalaxALT

This comment has been minimized.

Show comment Hide comment
@PikalaxALT

PikalaxALT Jul 12, 2017

Step 1 does not create .git/lost-found in git version 2.13.2 on cygwin.

Step 1 does not create .git/lost-found in git version 2.13.2 on cygwin.

@trevorwhealy

This comment has been minimized.

Show comment Hide comment
@trevorwhealy

trevorwhealy Jul 25, 2017

Thank you so much.

Thank you so much.

@mbenalirichard

This comment has been minimized.

Show comment Hide comment
@mbenalirichard

mbenalirichard 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>

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

This comment has been minimized.

Show comment Hide comment
@damusix

damusix Aug 4, 2017

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

damusix commented Aug 4, 2017

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

@mberrueta

This comment has been minimized.

Show comment Hide comment
@mberrueta

mberrueta Sep 21, 2017

thanks man! awesome!

thanks man! awesome!

@lukeoliff

This comment has been minimized.

Show comment Hide comment
@lukeoliff

lukeoliff Nov 3, 2017

Very helpful... πŸ‘

Very helpful... πŸ‘

@ilterocal

This comment has been minimized.

Show comment Hide comment
@ilterocal

ilterocal 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.

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

This comment has been minimized.

Show comment Hide comment
@waqashamid

waqashamid 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?

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

This comment has been minimized.

Show comment Hide comment
@CheragV

CheragV May 2, 2018

Thanks a lot !! learned something new here πŸ‘

CheragV commented May 2, 2018

Thanks a lot !! learned something new here πŸ‘

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