I had to recover a few git bare repositories from a backup, and found out that git (I am using msysgit on wondows XP) did not recognise the repo as such. After some investigation, I found the problem seems to be that the folder "refs" was missing, together with the subfolders "heads" and "remotes". I also do not have the file "heads/master". Is there any way I can re-create it? I really need to recover the data in these folders.
-
how about just `clone` repository again? Or you can `init` new repository – Sergey Gavruk Jul 22 '12 at 22:39
2 Answers
If you lost all your references (refs folder), but don't lose any actual data you can find all possible last commits by finding dangling commits (i.e commits which are not referenced by other commits as parents)
You can do it with the help of this command
git fsck --lost-found
This lists all dangling commits (and probably some other useful information). Use
git log SHA1SUM
to investigate further. Also it is a good idea to run
git fsck
to make sure that only referenced where lost.
UPD: Also refs can be located in packed-refs file.

- 1,561
- 9
- 11
Of course, if you have any checkouts of these repos, the easiest thing to do is to just recover from those.
refs
just contains files with the SHA1 of commits that those names point to; refs/heads/master
is probably all you need, containing just the SHA1 of the commit for HEAD. You didn't lose any data (that's all in objects
), but it might be a bit tricky to find out the SHA1 of the commits you want.
If you have the file logs/HEAD
, the ref should be in there at the bottom (the first SHA1 being the parent of the most recent commit and the second being the id of it; there might be more if it's a merge commit, not sure). For example, here's the last line of one of my repos:
4b2ef6873c3f4c7eaebca06fee4b95ffa9cf58c3 feb84419b6685b920f8a3d61a77e9508ba5dcfe1 Dougal Sutherland <dougal@gmail.com> 1342838724 -0400 pull: Fast-forward
so the hash starting with feb84419b6
is the one I'd want. Unfortunately, I think these are only in non-bare checkouts.
If you don't have those logs, the commits are all in the objects
directory. It's somewhat likely to be a recently modified one, which you can get on Unix for example by ls -1t */* | head -10
from the objects
directory; you can try a couple and see what happens. If you get from this, say
04/718652eb1f03bb0594cc05665665b749b52a87
then remove the /
to get the hash you want to use:
04718652eb1f03bb0594cc05665665b749b52a87

- 28,423
- 6
- 90
- 122
-
I have no **logs** folder. I only have **hooks**, **info** and **objects**. I am not sure if this is because this is a mirror repository, but does it mean I am out of luck? Any other place where I can get the SHA1 of the last commit? – LittleFish Jul 22 '12 at 22:50
-
@LittleFish Does it work if you use the name of the most-recently-modified object? – Danica Jul 22 '12 at 23:05
-
@Dougal, The problem is that I do not know which one is the most recently-modified. I just tried, but all the files under objects have the same time-stamp – LittleFish Jul 22 '12 at 23:18
-
1OK, with some tips from here, I managed to find the post [Recovering Git Repository from objects only](http://stackoverflow.com/questions/5767593/recovering-git-repository-from-objects-only). Because I had only few commits, it was easy to get the right one from git fsck. Then I did as you suggested to get that SHA1 into the .git/refs/heads/master file, and it worked!!!!Thanks a lot – LittleFish Jul 23 '12 at 08:17