Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring Entire Project from .git folder [duplicate]

I have a web project which I have been working on for some time. I use GIT with SourceTree to keep track of changes but foolishly had no proper backups in place.

I had about 50 commits and wanted to wipe the slate clean and start with a new initial state so I deleted the .git folder, however I messed things up in SourceTree which ended up deleting all the project files (although the folders remain for some reason).

I still have the .git folder which I deleted. My question is, can I recover the last known project state from this folder, and how would I go about doing this?

like image 535
Mike Emery Avatar asked Dec 24 '16 11:12

Mike Emery


1 Answers

Say you have a folder like this (assuming windows here, but makes no difference):

c:\myrepo\.git

with "myrepo" being empty otherwise, just having .git folder which has its full contents, you can just do

cd c:\myrepo
git reset --hard HEAD

Which should recover all your files.

Note: Advise to do "CD into the .git folder and type git status" is wrong, that is expected to reply with "Fatal - this operation must be run in a work tree".

like image 142
eis Avatar answered Oct 13 '22 16:10

eis