Restoring Entire Project from .git folder

9.9k Views Asked by At

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?

2

There are 2 best solutions below

11
On

If you really have the folder containing .git, then as far as I know all you have to do is restore this folder and Git should recognize it as a repository. If you also messed things up in SourceTree, then you delete the affected branches, or even create a new repo, and use your .git as the source for information.

1
On

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