18

I was fixing my packages and what not on Eclipse as I had too many redundant ones. A rookie error led me to deleting my .git folder from my drive. Is there any way I can recover my history from github? My repository is still active on github itself, but the .git folder is no longer on my pc monitoring the files I want to version control.

I'm new to version control so apologies in advance if I got any technical terms wrong.

  • 1
    Can you undelete your local `.git` folder? This seems like the easiest solution to me. If you can't undelete, then you may have to create a new repository on your local machine and pull the branches again. – Tim Biegeleisen Jun 23 '15 at 04:42
  • 3
    Nope - first thing I tried. Although I think I found a solution. I managed to clone it using the git GUI application. There were quite a few folders and my older .java files as opposed to the new updated ones after cloning. Just extracted the .git folder, copied it into my new package, deleted the cloned folder and seems to have done the job. –  Jun 23 '15 at 04:44
  • Git is pretty robust with regard to mistakes like this. Even if the FBI scorched your laptop, the most you would lose is the work you have done in a branch since the last push, which probably is only a day or less. – Tim Biegeleisen Jun 23 '15 at 04:45
  • Open the Windows Recycle Bin (assuming you are on Windows) and look for the `.git` folder. If you _just_ pressed delete in the Explorer window, you can press `CTRL + Z` to undo that action. +5 points coming at you. – Tim Biegeleisen Jun 23 '15 at 04:48
  • I dragged and dropped files/deleted packages using Eclipse as opposed to file explore rand for some reason it didn't go to recycle bin. Is cloning the next easiest way? Just clone from github into the respective folder? Recycle bin was the first thing I checked. –  Jun 23 '15 at 04:49
  • Yes, if you cannot undelete the `.git` folder then your only other option to `git clone` the repo again. – Tim Biegeleisen Jun 23 '15 at 04:49
  • Okay, thanks alot for your informative comments :) –  Jun 23 '15 at 04:50
  • 1
    `cd path/where/you/want/the/repo` followed by `git clone https://github.com/libgit2/libgit2`. Replace the path with whatever you want, and replace the URL with the correct one for your repo on GitHub. – Tim Biegeleisen Jun 23 '15 at 04:54
  • Okay, thanks alot. I was doing all sorts of things such as entering my username, branch, etc, this worked :) –  Jun 23 '15 at 05:11

2 Answers2

40

This is like Ajay's approach but will restore the .git folder in place, no need to clone and manually copy.

cd <repo-folder>
git init
git remote add origin <url>
git fetch
git reset origin/master

Same caveat applies, this will not work if you had changes in other branches which you didn't push to your remote (afaik they were lost when the .git folder was deleted, but someone may know better than I do on that), but it will not reset your local changes (if any) on your local copy, so you can then add and commit as needed.

If you do have local changes you want/need to keep, whatever you do, do NOT git reset --hard

Bob Davies
  • 2,229
  • 1
  • 19
  • 28
  • This has worked for me After giving the last command above, if you need to push your later changes to your remote repo, you should run `git push --set-upstream origin master`to set the new reference for your later pushes. After doing this you can continue to push your changes just by using `git push`. – Mycodingproject Feb 19 '20 at 12:43
4

it will be pretty easy

Create a new repository conning from your github repo from the branch you are in

git clone <repo_url> -b <branch_name>

Now to get your changes back , copy the contents of your previous repository in to this one. Your repository would have the same set of files. Hope this helps.

PS : this will not work if you had changes in other branches which you didn't push to your remote. Also this would erase all the commits from your current branch which you had not pushed to remote but you will retain all your local changes.

Ajay
  • 2,976
  • 2
  • 28
  • 35
  • 1
    I think I did mine a different way. How do I copy the contents of one repository from github to another? I just used the git GUI, and my old repository was up there from github, cloned it to the source folder on my desktop and seemed to have fixed the problem after extracting the .git folder. –  Jun 23 '15 at 05:13
  • oh! I think that is a better way :) – Ajay Jun 23 '15 at 05:18