7

Due to some error in code, we lost a complete GIT-controlled directory. Restoring the files was not a problem; TimeMachine took care of that. However, TimeMachine apparently did not back up the .git folder.

Is there a better way to restore/recreate the .git folder than to fetch the directory from the master server or another machine?

Thanks in advance for any useful hint.

gturri
  • 13,807
  • 9
  • 40
  • 57
Max Wyss
  • 3,549
  • 2
  • 20
  • 26
  • 5
    If you have pushed all changes why hesitate to use your master to fetch your .git folder? – flob Jul 16 '14 at 08:04
  • "TimeMachine apparently did not back up the .git folder." That's a scary thought. Can someone confirm this? How about other dot-directories? – Thilo Jul 16 '14 at 08:05
  • A quick google search shows that Time Machine must be manually configured to avoid backing up .git directories, so unless you have purposely set it up that way, I wouldn't worry about it. – Rob Wise Jul 16 '14 at 08:07
  • Update: the .git directory did get restored. Not noticing it is my fault. However, somehow the .gitconfig file got messed up and I get a "malformed" error message… further investigations. So, thanks for the help in any case. – Max Wyss Jul 17 '14 at 07:29

2 Answers2

19

You can check out a bare .git repo and then inflate it into a full repo by adding your source code. This will still download the entire .git folder but not your working copy code files.

  1. Create a new folder and navigate to it.
  2. Clone a bare repo:

    git clone --bare https://path/to/project .git

  3. Copy your locally recovered files around the .git folder (in the same relative location as they were earlier).

  4. Mark the new repo as non-bare:

    git config --local --bool core.bare false

  5. Finally reset the index:

    git reset HEAD -- .

metacubed
  • 7,031
  • 6
  • 36
  • 65
  • 1
    Isn't it more easy to clone normally to a new spare directory and move the ".git" folder from the spare directory to the project folder (where it was originally)? – Readren May 28 '16 at 08:34
  • 2
    @Readren the question asked for restoring the .git directory without fetching everything from the server. – metacubed May 28 '16 at 08:38
2

There are 2 options:

1. restore it form backup (which i assume you don't have) 
2. clone the repository again and compare the content to add the modified content.

Since .git is simply folder with content.

If you did not backup it with Time Machine so you will have to clone it again.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167