Meir
Meir

Reputation: 235

Recovering git repository from the objects directory

I don't know how it happened, but I had a data loss and was only left with a .git directory containing only an objects directory.

I've followed the steps from Recovering Git repository from objects only:

  1. Created a new directory and ran git init
  2. Copied the objects directory I had to the new .git/objects directory
  3. Ran git fsck command.

The output of the above steps produced the following msg:

notice: HEAD points to an unborn branch (master)
notice: No default references
dangling commit 0b2c7d52b6a7e6d4e2858e9ebf207c315407c87c
dangling commit 8dceafea1634c923069f6d4b925839c28d92c4e5
dangling commit dd7bdd04c557a018c15fd0948075121f181decd1

When I try to run git log at this stage I get fatal: bad default revision 'HEAD'.

What should I do in order to recover my data???


EDIT:

klaustopher's suggestion:

  1. I added a file under /refs/heads called master containing one of the dangling commits.
  2. The msg I got in return was fatal: unable to read tree 3c864da48b16ad0dc5f8ae585380270a708a1e56
  3. I checked and there is no 3c directory under "objects"...

Upvotes: 1

Views: 2052

Answers (1)

klaustopher
klaustopher

Reputation: 6941

Well, you got the objects but git has no idea which is the commit it should interpret as the master. There are three suggestions in the output of git fsck. You could try the following:

  • create the .git/refs/heads directory and create a file called master
  • Put the SHA1 hash of one of the commits in this file
  • Run git checkout master

If that is not the commit you were looking for, put in another sha1 hash in the master-file

--

Edit: If those aren't real commits like you described in the comment, than you should maybe go through your objects. With git show 2452845a7b526db3dfbf88c1c2ca05967ef9653a you can see the contents of the file. If it is a commit it will start with the word commit and also have the date, commit message, etc in it. You could maybe write a little shell script to go through all the objects. When you found your commit, put its sha1 in the refs/heads/master file and do what I described above

Upvotes: 1

Related Questions