Git repository broken after computer died

Git

Git Problem Overview


My computer went dead and now one of my git repositories is broken. When I try to checkout master it tells me:

warning: ignoring broken ref refs/heads/master.
error: Your local changes to the following files would be overwritten by checkout:
        com.vainolo.jdraw2d.releng.p2/pom.xml
Please, commit your changes or stash them before you can switch branches.
Aborting

When I execute git stash I get:

fatal: bad revision 'HEAD'
fatal: bad revision 'HEAD'
fatal: Needed a single revision
You do not have the initial commit yet

So... what can I do?

Update Output of git reflog:

fatal: bad default revision 'HEAD'

Not very promising... Output of git fsck:

error: Invalid HEAD
Checking object directories: 100% (256/256), done.
error: unable to unpack 59551f96b4e87a1c14293c19eb548ce6fa1f196f header
error: inflateEnd: stream consistency error (no message)
fatal: loose object 59551f96b4e87a1c14293c19eb548ce6fa1f196f (stored in .git/objects/59/551f96b4e87a1c14293c19eb548ce6fa1f196f) is corrupt

Git Solutions


Solution 1 - Git

I managed to recover through:

rm .git/refs/remotes/origin/HEAD
git fetch --all

Solution 2 - Git

Start by following the steps suggested in Recovering broken git repository:

  • check whether .git/refs still contains anything useful
  • check git reflog and failing that the contents of .git/logs/refs/heads/master or whatever branch you were on last
  • run git fsck, potentially with --unreachable or --lost-found

This will hopefully allow you to figure out what the master ref should be so you can restore it (i.e. cat the correct SHA1 into .git/refs/heads/master).

In case any object contained in that commit is genuinely corrupted you can't restore your HEAD commit unfortunately. Assuming your working tree and/or index are intact you can try a git reset --soft (or failing that a git reset) to the previous commit and then re-do the commit. Avoid any operations that change your working tree s.a. git checkout -f or git reset --hard.

Solution 3 - Git

I had a similar issue following a blue screen of death on windows 8.1

I had a file in this location...

C:\www\<project>\.git\refs\remotes\origin\<problem-branch>

And it was empty whereas the other branch files in this folder has long strings inside of them.

NB I didn't have any changes/commits

  • I backed up the <problem-branch> file
  • Deleted the file
  • git fetch --all to get the branch again

Then the tab auto completion started working again

Solution 4 - Git

If there is not many modified files, I think the convient way to solve this problem is:

  1. backup the files you modified in the repo
  2. remove your existing repo
  3. re-clone it from server
  4. paste the files from step 1 to the repo, and git commit -a

Solution 5 - Git

After a computed freeze and crash, my git branch was damaged with the message: git fatal: your current branch appears to be broken. I could not do anything.

After doing git fsck mentioned that the branch had an error: Invalid HEAD. refs/heads/<branch> had an invalid sha1 pointer.

Upon following the options here, I opened .git/refs/heads/<branch> in an notepad++ editor, and each of the sha1 characters were NUL.

Fortunately I only needed to reset the branch to remote state, and that was on a bitbucket repo. I grabbed the sha1 from the tip of the remote repo and copied to the .git/refs/heads/<branch> saved it, then did a git reset --hard HEAD, and everything back to normal.

Solution 6 - Git

i managed to solve this with deleting master file in git\refs\heads directory

Solution 7 - Git

I could not checkout my master branch due to the cannot lock ref error. I ended up deleting: .git/refs/remotes/origin/HEAD .git/refs/remotes/origin/master

and calling this git command:

git fetch --all

Solution 8 - Git

I was idiot enough to forget to push and my computer crashed while performing a commit. I could recover everything but the last commit though by opening .git/logs/refs/heads/

This file contains all commits (with their SHAs) to the branch, what I did to recover was:

  • Back up latest changes to a temp folder
  • move to a "clean slate"
  • git checkout master
  • git reset --hard
  • checkout the second to last commit in the log
  • create a branch from this detached head
  • PUSH
  • Restore the latest changes
  • Commit again

So even when you make a dumb mistake, you're not immediately taken back by a whole day of work with git :)

Solution 9 - Git

I had this same problem when Android Studio terminated suddenly (due to loss of power to computer).

I solved it by copying the contents of my C:\Users\myusername\AndroidStudioProjects\MyBrokenApp\.git\refs\heads\master file to my C:\Users\myusername\AndroidStudioProjects\MyBrokenApp\.git\refs\remotes\origin\master file.

(Previously, I'd also turned on the 'Force Push' option in Android Studio, but I don't think this was a necessary step.)

Note:

I found this solution by comparing the content of the files in my C:\Users\myusername\AndroidStudioProjects\MyBrokenApp\.git\ directory (inc. subdirectories) to the corresponding files of those in another healthy project - e.g., C:\Users\myusername\AndroidStudioProjects\MyHealthyApp\.git\.

You may have a different file that is corrupt, but by comparing with another healthy project, you should be able to quickly spot what is wrong.

If you don't have another healthy project that has git configured, it may be worth creating a simple one in the same way that you created your broken project so can investigate, compare and fix, etc.

PS - My error message (edited) was: warning: ignoring broken refs/remotes/origin/master.fatal bad revision 'refs/remotes/origin/master..refs/heads/master' during executing git -c core.quotepath=false log refs/remotes/origin/master..refs/heads/master --pretty=format --encoding=UTF-8 -M --name-status -c --

Solution 10 - Git

I know it's a too-late response, but I was getting this error because I did not have an origin/head. You can find this out by running git branch -r. If you do not see your origin/head pointing to a remote origin, you can set this by running git remote set-head origin {{your branch name}}.

Now run git branch -r again, and you should see something like this: origin/HEAD -> origin/develop

I hope this helps anyone else who's running into this issue.

Solution 11 - Git

My computer crashed two times and a result, my git repository got broken locally. I could not pull my changes, it asked to set remote origin but it did not work in gitKraken.

On my command prompt, I was getting this error enter image description here

I knew that my references are broken and needs to be fixed. What I had to do is, to git bash (In SourceTree click on "terminal"). Then navigate to references folder like this

cd .git
cd refs
cd remotes
cd origin

there will be a file name master there, use ls to see what is in the directory. Then simply delete it using rm master

bam, the corrupted file is gone. Now if you issue git branch command -a, it would output this

$ git branch -a
* master
  remotes/origin/master (this in red color -scary :) )

Then issue this command, and it will fix your references

$ git remote set-head origin master

To sum it up, if you would try to pull the remote, it should show blank remote name which has been fixed.

enter image description here

Solution 12 - Git

I had the same issue but no luck, couldn't figure out the problem. I took my repo to a side, re-cloned the one from server and tried to merge between them. Of course it shown a lot of files not related to my branch, but help to isolate the files required.

Solution 13 - Git

Check if MSWindows created desktop.ini files that are bothhering the git? it does for me. Once I delete them all in the subfolders of the .git directory then it works.

Solution 14 - Git

Forgive me if I would repeat after someone (I haven't read all feedback). In my opinion, the simplest way to solve the problem is to copy project without .git and .idea, clean it up, clone from git, delete everything except above directories and then paste previous copy to newly created repo with .git and .idea. Hope it does make sense.

Solution 15 - Git

Since I knew my local branch was up-to-date, a quick fix was to simply copy the head SHA-1 to the remotes.


My Possible Cause: power failure.

I was getting this error but I was able to push changes anyway.

I checked the file .git/refs/remotes/<branch> and it had all null characters.

However, .git/refs/heads/<branch> had the correct SHA1.

Solution 16 - Git

Just adding my case:

git push

> error: cannot lock ref 'refs/heads/master': unable to resolve > reference 'refs/heads/master': reference broken

I tried:

git clone <path>

But got:

You appear to have cloned an empty repository.

I checked this file on server repository:

> refs/heads/master

The file has a long blank-caracters-string.

I checked the last commit identifier with this command (fortunately it was still working):

git log origin/master -5

Then I copied the last commit identifier to the server file refs/heads/master.

Risky, but it worked. Clone, push and pull are ok until now.

Solution 17 - Git

If you remember your head was pointing to which branch then just go to that branch on github/bitbucket and copy the hash of the latest commit on the branch and paste it into file .git\refs\remotes\origin\HEAD and then the same git add . git commit -m "" git push

Solution 18 - Git

Facing a similar issue (computer emergency rebooted, presumably due to memory leak/BSOD from Sourcetree using Windows bugchecker re: KB4586853?) I got the following from git fsck --full:

error: refs/heads/merge/FEED-318: invalid sha1 pointer 0000000000000000000000000000000000000000
error: bad ref for .git/logs/HEAD
error: bad ref for .git/logs/refs/heads/<my-branch-name/with-subfolder>

Not sure if it was coincidence but my branch was named something like merge/TICKET-NO.

After blindly trying many of these solutions I found the SHA of the previous commit (from .git/logs/HEAD) and just --force checked it out, problem basically solved. All my history came back, was able to gitk and see the pretty graphs, etc. Only lost the changes for the last attempted commit.

Solution 19 - Git

Detailed Explanation

What is the meaning of the error message "Broken head"?

Before solving the problem, we should understand the error message first.

The error message says the HEAD is broken! Which HEAD? The head means our current branch. The error basically means CURRENT BRANCH is broken.

Let's say we have branches like ab, bb, cb and current branch is ab branch.

When we run this command ls .git/refs/heads, we'll see ab, bb, cb, which are the files that contains a hash value like 392b55ccd3ba02fe236148c5264f8ef1da.

Whenever we commit, this hash value changes.

Let's go back to the error message, "Head is broken".

It actually means the hash value of this branch is NOT VALID!

How to regenerate this error (maybe)?

Step 1 : Make lots of changes without committing.

Step 2 : Shutdown your computer just 1 second after committing your big changes.

So, git could not update the new hash and now the old one goes invalid!

How to solve?

Just delete the current hash value! That simple!

The ab is our current branch, which is our broken head!

rm .git/refs/heads/ab command will delete the ab file, which contains the INVALID hash value.

If the branch is remote branch,( which is pushed to repository before ), then we should git pull --allow-unrelated-histories --ff origin ab

If the branch is local, then keep committing.

Solved!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvainoloView Question on Stackoverflow
Solution 1 - GitjfrumarView Answer on Stackoverflow
Solution 2 - GitkynanView Answer on Stackoverflow
Solution 3 - GitCarltonView Answer on Stackoverflow
Solution 4 - GitalsotangView Answer on Stackoverflow
Solution 5 - Gitj4v1View Answer on Stackoverflow
Solution 6 - GitEmmanouil GaitanakisView Answer on Stackoverflow
Solution 7 - GitKeon SadatianView Answer on Stackoverflow
Solution 8 - GitMarkus PalcerView Answer on Stackoverflow
Solution 9 - Gitban-geoengineeringView Answer on Stackoverflow
Solution 10 - GitaaronwbrownView Answer on Stackoverflow
Solution 11 - GitHammad KhanView Answer on Stackoverflow
Solution 12 - GitSamuelensView Answer on Stackoverflow
Solution 13 - GitvinniefView Answer on Stackoverflow
Solution 14 - GitAndrey SulayView Answer on Stackoverflow
Solution 15 - GitSergio CarneiroView Answer on Stackoverflow
Solution 16 - GitheringerView Answer on Stackoverflow
Solution 17 - GitDevbrat DashView Answer on Stackoverflow
Solution 18 - GitdrzausView Answer on Stackoverflow
Solution 19 - GitAhmet EmrebasView Answer on Stackoverflow