63

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 ?
Yves M.
  • 29,855
  • 23
  • 108
  • 144
bicepjai
  • 1,615
  • 3
  • 17
  • 35

5 Answers5

80

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 git@github.com: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

laplasz
  • 3,359
  • 1
  • 29
  • 40
Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90
  • 7
    If you had local *unpushed* changes before you trashed `.git`, you should keep the working copy safely and then patch back the changes after you clone from github. – Noufal Ibrahim Jun 06 '11 at 05:13
  • 17
    To expand on the above, if you have uncommitted changes in your working copy you 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. – Tekkub Jun 06 '11 at 20:00
  • 1
    @Tekkub added your suggestion to answer. Thank you! – Mak Feb 03 '17 at 21:05
  • 1
    Actually, it should be `git reset`, without the `--soft`. That will also reset the index, and you can then continue adding changes you want to commit as usual. – jthill Feb 03 '17 at 21:10
  • 3
    Or clone to another directory and move `.git` subdirectory where it should be. – Kornel Feb 03 '17 at 21:10
  • brilliant answer..got me out of a big rut! – huzefam Aug 22 '17 at 10:37
  • `git reset` does nothing because you are on local, empty master. – Nakilon Sep 29 '20 at 13:35
  • I want to add that it may be possible from just file recovery (no backup/trash) depending on what was deleted and what is recoverable https://stackoverflow.com/a/65181667/1507124 – CervEd Dec 07 '20 at 12:26
  • @CervEd This is the very last resort as it's not guaranteed in any way. – EvgenKo423 Apr 23 '21 at 18:22
  • @EvgenKo423 no ofc, I've had it both succeed and fail. But if you've reached this comment you've probably run out of other options. – CervEd Apr 24 '21 at 06:18
  • `git push --set-upstream origin main` You might want push like this (only your first push after re init). – Noel Schenk May 30 '22 at 08:50
39

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

mimipofi
  • 655
  • 6
  • 6
4

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.

Dennis Persson
  • 843
  • 7
  • 23
  • There will be nothing staged, `git rebase --autostash` will say `Current branch master is up to date` and do nothing, because your commands don't restore the index and don't stage anything. Also there's no need in rebase if the working tree is ahead of remote, but if _remote_ is ahead of working tree then you'll have to [figure out the state of a local repository](https://stackoverflow.com/q/49059767/11725753) first. – EvgenKo423 Jun 12 '21 at 08:56
  • You are right that the index isn't restored. I edit it and add git reset HEAD -- . – Dennis Persson Jun 21 '21 at 15:51
1

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

Shreyansh Panchal
  • 827
  • 12
  • 22
-2

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.