I have a local Git repository that I keep a backup copy of as follows:

cd /path/to/local/repo
git init
git add -A
git commit -m "Initial commit"
git clone --bare /path/to/repo_backup/my_repo.git
cd /path/to/repo_backup/my_repo.git
git init --bare --shared
cd /path/to/local/repo
git remote add origin /path/to/repo_backup/my_repo.git
git push --set-upstream origin master

While I was out for two weeks, our IT department unilaterally decided to delete the .git directory that was my local repo. Changes were then made to the working directory, and I am unable to run git status to see what changes were made.

I need to restore the local repo from the backup (i.e. remote) repo without affecting the working directory (i.e. so I can't do a straight git clone).

I have attempted this via:

cp /path/to/repo_backup/my_repo.git /path/to/local/repo/.git
cd /path/to/local/repo

However when I run git status, I get this error:

fatal: this operation must be run in a work tree

How may I properly restore the local repo without affecting the working directory?

  • There’s git clone --no-checkout, by the way. You could then use git symbolic-ref to proceed. – Daniel B May 17 at 12:11

The cp command you've shown lacks the -r flag, so I'd expect it to fail to copy the .git directory.

Could it be that you copied your new .git directory into the old .git directory, rather than to replace the old one? (That is to say, to accidentally create a .git/.git subdirectory)

Perhaps try:

mv /path/to/local/repo/.git /path/to/local/repo/TEMP-old-dot-git
cp -r /path/to/repo_backup/my_repo.git /path/to/local/repo/.git

Also, although we can convert between bare and non-bare repos simply by renaming, doing so relies on git internals, and it's possible git won't always work this way. Wise git users recommend using git clone rather than renaming.

I can't do a straight git clone

You could if you cloned into a temporary directory and then moved the .git directory. The clone operation itself will refuse to collide with existing files anyway.


Lack of -r switch in cp command is likely culprit.
Regarding impossibility of git clone to restore local copy - you could do it by:

  1. git clone "remote" backup to a different directory
  2. Move your working directory to said different directory (while overwritting all files)
  3. Move whole thing back to original path

... but it doesn't make much sense if you can recover .git directory.
That said, using local git repo as a backup has it weaknesses (that you already experienced); you could consider checking Git Backup Guide for details on good backup practices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.