Git corrupt master branch
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
Solution 1:
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/master
is 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
.
Solution 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'
Solution 3:
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.