tags:

views:

210

answers:

3

My working git repository is broken, it lose track to all the files in it, i.e.

$ git log
fatal: bad default revision 'HEAD'  
$ git status  
... told me that all the files are new

However the .git directory does contain my objects.

$ du -sh .git
34M    .git
$ git count-objects
4151 objects, 32692 kilobytes
$ git --version
git version 1.6.0.4

The last thing I remember doing before it went wrong was creating (clone --mirror) a backup repository at an NFS-mounted server. However the backup repository cloned is broken the same way.

How can I restore my repository?

+2  A: 

There must've been something besides the clone, but I know how hard it is to remember those things.

The first thing you want to do is look in .git/refs and see if there's anything valid in there (I'm not too optimistic since you say that there don't appear to be any branches, but it's worth a shot). If any valid refs exist, you may be able to get some information from git-reflog.

Next, I'd start having a look at git-fsck. Its main purpose is to verify connectivity and validity of objects in the database. Depending on what exactly has happened to your repo, you may need --unreachable or --lost-found. Hopefully the objects are intact, so all you need to do is find some dangling commit hashes to check out and recreate branches at.

Jefromi
Thanks. I have fixed the repository manually. It turned out that the branches (files in .git/refs/heads) were missing, but the objects were intact. I was able to get commit hashes of the tip of each branch from .git/logs/HEAD, and used them to recreate the branches files. I didn't know the git-fsck command then.However I'm still curious how did this happen, obviously I didn't do <pre>git branch -d</pre>. I don't know many git commands, the only ones I remember doing were <pre>git reset</pre> and <pre>git remote rm ...</pre>
Eyoka
+1  A: 

You can examine manually, but that would require some knowledge on the format of the repository.

Without looking at the repository is hard to tell what is happening, but probably some file was corrupted.

Run git fsck and it will tell whether your repository is still valid.

Post the result of the git fsck run and that should help us to help you.

+1  A: 

Try to check if each of your files in .git/ are owned by current user.

I had the same problem, when realizing I made some commits with root user, and that created objects (under .git/objects) where belongig to root, trigering errors when running git as regular user.

This command solved the problem:

sudo chown .git/ jb:jb -R *
Jb