9

I open my Git repository using gitExtensions on Windows 7 for a Visual Studio project. It is suddenly empty. The repository exists, but all my commits have disappeared.

I am using the graphical interface and I believe this is the first time I've opened it since they updated it.

I'm unsure what to do about getting back my commits.

When I type

git log 

I receive

fatal:bad default revision 'HEAD'

Update
After looking at https://stackoverflow.com/questions/1545407/recovering-broken-git-repository i tried

git fsck

it returned:

error: Invalid HEAD
fatal: loose object 36b7d9e1ca496bcb864c0b9c8671fcec97fbda31 (stored in .git/obj ects/36/b7d9e1ca496bcb864c0b9c8671fcec97fbda31) is corrupt

Committing returns:

error: unable to resolve reference HEAD: No such file or directory
fatal: cannot lock HEAD ref

and logging master branch returns

$ git log master warning: ignoring broken ref refs/heads/master. warning: ignoring broken ref refs/heads/master. fatal: ambiguous argument 'master': unknown revision or path not in the working tree. Use '--' to separate paths from revisions

Ill just keep pasting things that could be relevant

$ git reflog master
warning: ignoring broken ref refs/heads/master.
warning: ignoring broken ref refs/heads/master.
fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

More possibly useful info: every single time i delete the corrupt file another one takes it's place. Im beginning to think its something to do with the master branch pointing to the wrong thing or something. because i assume the head is pointing to master.

One day later:
So I got my mate onto this, he was able to go through the logs and he said that the hashes in the logs don't match up to the objects in the folder. He tried resetting the master branch to the logs or something like that, I got a bit lost. Hope thats helpful

2

4 Answers 4

3

The repository exists, but all my commits have disappeared.

What exactly do you mean? Is the working tree still there? Does .git/ exist? Are there any files in it?

The messages you posted suggest that the file .git/HEAD does not exist. It defines the expected state of the working tree (what you had checked out). If that file is gone, git doesn't know where you were.

You could try creating the file yourself, with this content: ref: refs/heads/master

If you were on a different branch, just replace "master" with the branch name. If you were not on a branch, it would be more complicated.

.git/logs/HEAD records past states of HEAD, with later lines at the bottom. This example line shows a checkout: 25f2a6099fb5f9f2192a510c42f704f9fc4bcecb 65abb1a3dc102e2498860f01fb179cda4c51decb Rainer Blome <[email protected]> 1346938344 +0200 checkout: moving from master to MySuperBranch

The SHA1s in front refer to commits. You ought to be able to find these in the branch log, for example .git/logs/refs/heads/master.

The git reflog output you gave looks like refs/heads/masteris missing as well. Its sole content is supposed to be the SHA1 of the latest commit on it (and a newline). You can find the latest SHA1 at the end of the branch log, for example .git/logs/refs/heads/master.

2

If .git/HEAD exists and its content is ref: refs/heads/master then checks the file refs/heads/master it must contains the sha1 of the last commit.

If that file was corrupted and full of NULL characters Edit that file and put the sha1 of the latest commit from .git/logs/HEAD or the one before the last commit.

Then do git reset --hard 'sha1 of the commit that you selected'

1
  • it was indeed full of NULL chars so I put in the previous commit's sha1, but then doing the git reset resulted in "error: update_ref failed for ref 'HEAD': cannot lock ref 'HEAD': unable to resolve reference HEAD: Invalid argument" Dec 14, 2017 at 14:33
1

It sounds like your repo has been corrupted. The easiest thing to do would be to recover your repo from a backup or re-clone the repo from the original source (assuming you didn't have tons of work in the repo).

If resotring/cloning is not an option I would recommend reading through Pro Git (free online book or the paper version). The whole book is very informative, but especially take a look at the last chapter to get an understanding of how Git works internally. Once you have an understanding of how Git works, take a look at Linus' instructions on recovering corrupted objects.

2
  • So, sadly I wasn't backing up hidden files, .git was hidden. I don't really have enough time to read an entire book, is there anything you think I could attempt?
    – MrJD
    May 10, 2012 at 4:55
  • "Read a book about the internals" might be sound general advice but it doesn't help address the specific problem and question at hand.
    – Burhan Ali
    Aug 28, 2012 at 22:47
0

After surfing through the web for a while, I finally found this and it worked.

git fetch origin
git reset --hard origin/master
1
  • That gets any changes from origin (hopefully not too much work done locally), and then forces the local master branch to agree with the remote. Careful, --reset means discard any local changes! Besides, if it wasn't too broken, just git reset origin/master would have restored the last known (checked in) state of the master branch.
    – vonbrand
    Feb 20, 2020 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .