Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to restore .git folder

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.

like image 775
Max Wyss Avatar asked Jul 16 '14 08:07

Max Wyss


People also ask

Can deleted .git be restored?

To get the Git undo delete file, you need to do a reset. The action of reset will restore the data to a state ere you commit. This action, however, has a disadvantage. It may delete other changes made to the file after the commit.

What if .git folder is deleted?

The folder is created when the project is initialized (using git init ) or cloned (using git clone ). It is located at the root of the Git project repository. If we delete the . git folder, the information saved by Git will be lost, and the directory will no longer act as a Git repository.

Can I just copy the .git folder?

Yes you can, but there's no need to copy the full working tree. You can copy just the . git folder without the working tree (i.e. as a "bare" repo) and then checkout the latest working tree on the other machine.


2 Answers

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

like image 91
metacubed Avatar answered Oct 18 '22 02:10

metacubed


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.

like image 2
CodeWizard Avatar answered Oct 18 '22 01:10

CodeWizard