LittleFish
LittleFish

Reputation: 61

Git - folder "refs" missing

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.

Upvotes: 0

Views: 4194

Answers (2)

Oleksandr Pryimak
Oleksandr Pryimak

Reputation: 1581

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.

Upvotes: 4

Danica
Danica

Reputation: 28846

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 <[email protected]> 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

Upvotes: 1

Related Questions