Can deleted .git be restored?

GitGithub

Git Problem Overview


This happened when working with git on Ruby on Rails. several commits and branches were done and created.

git version 1.7.3.4

I accidentally deleted the .git folder using

git -rf .git

but I have all the files and updates done on online git-hub repository. I want the .git folder restored. Questions ?

  1. Am I totally screwed up ? OR
  2. If theres a way to restore .git from my online git-hub repository, how can that be done ?

Git Solutions


Solution 1 - Git

The only way you can restore a deleted .git folder is by:

  • Cloning it again from somewhere
  • Checking your recycling bin or backup (if no remote repository exists)

Since your files are from the online github repository, then its simple. Just clone it again from the directory where you deleted the .git folder:

git init
git remote add origin <repo_address>
git pull origin master

where repo_address can be [email protected]:yourname/yourproject.git

Your local checkout will be back to normal.

If you have uncommitted changes in your working copy you would want to keep, instead of using git pull use git fetch and then git reset --soft your local branch to the remote branch it should be at. The soft reset will not change your working copy

Solution 2 - Git

You can clone your repository to another location and simply copy the cloned .git folder to the place of your deleted .git folder.

Solution 3 - Git

I think the best bet is to Clone the repo in a throwaway folder. And copy the .git folder from that to your desired folder location (The project folder in which you've deleted .git folder).

Solution 4 - Git

If you have uncommited changes in your repository you can do this.

git init
git remote add origin <YOUR_REPO_ADDRESS>
git fetch
git branch master origin/master 
git reset HEAD -- .
git rebase --autostash

git branch master origin/master creates a local master branch to track remote master branch. That is required because your new repo won't have a local branch until you have made a first commit.

Rebase will put your uncommited staged on top of the code you have fetched. Autostash option will temporary stash you edits and then put them back after the rebase has completed.

Worried? Make a backup copy of your project.

Solution 5 - Git

if you compiled your work and it is in a jar format. Go to your .m2/repository/location and find that jar file. change the .jar to .zip. unzip the file and your git repository should be there.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbicepjaiView Question on Stackoverflow
Solution 1 - GitMohamed MansourView Answer on Stackoverflow
Solution 2 - GitmimipofiView Answer on Stackoverflow
Solution 3 - GitShreyansh PanchalView Answer on Stackoverflow
Solution 4 - GitDennis PerssonView Answer on Stackoverflow
Solution 5 - GitChrista KingView Answer on Stackoverflow