lost9123193
lost9123193

Reputation: 11030

Fix Corrupted Loose Git Head in Windows

My computer shut down due to a kernal panic I lost my git repo on my virtual machine which is in Windows. When I go on .net I get the following error:

corrupted loose reference file: HEAD

I have in place a folder called .git that contains a list of my branches.

enter image description here Is there a way I can convert this back to a git entity? Or would I have to reclone the directory?

Upvotes: 5

Views: 7186

Answers (3)

Chris Sprague
Chris Sprague

Reputation: 3574

I fixed by doing the following:

  1. Zip your repo just in case you remove the wrong thing
  2. Use an editor (NotePad++ or VisualStudio Code) to search in all files for references to your bad branch. For example, in NotePad++ hit Ctrl+shft+F and looke for feature/my-bad-branch
  3. Open the files with that reference and decide if you should remove the reference
  • I only had to remove the branch from .git/config
  • I didn't delete any references to it in logs
  1. Look in the .git/refs/remotes/origin folder and subfolders for a file by the same name as your branch. For example .git/refs/remotes/origin/feature/my-bad-branch. Delete that file
  2. Try to pull from the origin again

Upvotes: 0

Nimeshka Srimal
Nimeshka Srimal

Reputation: 8920

You can set it to your previous stage without cloning the repository again. You might be able to keep your local changes, but you will lose all your unpushed commits or stashes.

This is what I would do:

  1. Take a backup of your git repository. (just copy it in case you need anything).
  2. remove the .git folder (in linux, rm -rf .git)
  3. git init
  4. git remote add origin <your remote repository url>
  5. git fetch
  6. git reset origin/<the last branch you were in>

This is important: If you were in the Development branch, then give it as origin/Development.

Now everything should be back in place. If you run git status, you should see your modified files etc.

This is only a suggestion. You can copy your folder and give it a try and see :)

Edit: I have missed to include an important step. You need to fetch after you add the origin.

Hope it would work for you :)

Upvotes: 2

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

What you're showing in your .git directory is not a list of branches; it's the standard structure for git repo metadata. This includes a few symbolic refs, such as HEAD. When a ref is stored as a file on disk, it's called a "loose" ref, which is what I believe the error message is talking about.

So the error seems to indicate that the file .git/HEAD is corrupt. That in itself is not hard to fix (though most git commands won't work since they won't currently recognize that this is a repo, so it will take a bit of a kludge); the bigger question, in the wake of a sudden system crash, is whether anything else is also corrupt. But as a starting point, you might try something like

# back up .git/HEAD somewhere, just in case
echo 'ref: refs/heads/master` >.git/HEAD

Now hopefully your repo will be recognized again. Next it would be smart to do some checks like

git fsck

and maybe checking if you have expected changes on expected branches.

The most vulnerable thing would be uncommitted changes (and especially unstaged changes). But at least if the above steps go well, you can assess the damage and decide where to go from there.

Upvotes: 8

Related Questions