Best way to restore .git folder

Max Wyss picture Max Wyss · Jul 16, 2014 · Viewed 11.1k times · Source

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.

Answer

metacubed picture metacubed · Jul 16, 2014

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