104

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
vainolo
  • 6,907
  • 4
  • 24
  • 47
  • Can you check if `.git/refs/heads/master` exists and if its content is a valid commit hash of your repository (you can check that e.g. using `git show `)? – poke Mar 09 '13 at 23:25
  • I know this is obvious, but still asking - do you have any remote repos of the same git repo ? – Tuxdude Mar 09 '13 at 23:25
  • @poke the contents of `.git/refs/heads/master/` are a bunch of `^@` – vainolo Mar 09 '13 at 23:30
  • @Tuxdude yep, but not updated to my latest changes – vainolo Mar 09 '13 at 23:31
  • 2
    What does `git reflog` tell you? Have you tried running `git fsck`? – kynan Mar 09 '13 at 23:31
  • @vainolo Okay, that sounds definitely broken. Can you check if `git reflog` still works and produces some output? Can you spot any version that might be the newest commit on master? – poke Mar 09 '13 at 23:33
  • Update question with the requested data – vainolo Mar 09 '13 at 23:43
  • Anything useful in `.git/logs/refs/heads/master`? – kynan Mar 09 '13 at 23:53
  • https://git.wiki.kernel.org/index.php/Git_FAQ#How_to_fix_a_broken_repository.3F – thameera Mar 10 '13 at 03:05
  • @kynan seems the `logs` does contain my last commit, but I can't check it out: `fatal: loose object .... (stored in .git/objects/...) is corrupt) – vainolo Mar 10 '13 at 06:29
  • That is in line with the `git fsck` output: that blob seems to be genuinely corrupted. In that case you can't restore your `HEAD` commit unfortunately. Assuming your working tree and/or index are intact try a `git reset --soft` to the previous commit and then re-do the commit. – kynan Mar 10 '13 at 10:33

20 Answers20

182

I managed to recover through:

rm .git/refs/remotes/origin/HEAD
git fetch --all
jfrumar
  • 1,970
  • 1
  • 12
  • 7
  • In my case it happened because I deleted some local and remote branches (somehow the .git/refs/remotes/origin/HEAD file remained in an inconsistent state). Changing the contents of the above mentioned file to point to an existent local branch (e.g ref: refs/remotes/origin/master) solved this problem. Still, the above approach might be better since HEAD might point to a commit not in the current branch. – crissdev Sep 27 '16 at 19:01
  • In case the problem happens to a specific git submodule, the first command slightly changes to `rm /.git/modules//refs/remotes/origin/HEAD` – Gobe Jul 07 '17 at 16:41
  • 4
    I had to do `rm -rf .git/refs/remotes/origin`, but u pointed me to the right direction – Jacka Oct 25 '18 at 09:56
  • In my case, i removed the whole **"origin"** folder, as there was no HEAD file. – Ayoub Laazazi Apr 08 '21 at 21:06
27

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.

Community
  • 1
  • 1
kynan
  • 13,235
  • 6
  • 79
  • 81
  • I looked at `.git/logs/refs/heads/mybranch`. It shows some kind of history of commits to this branch. Digging through that I picked out SHAs and tried to display them with `git show`. (Each commit has two SHAs, I picked the second, just before the author's name.) The last one was corrupt but the one before that could be `git show`n and I was able to then push it with `git push origin abcdef:mybranch`. – Ed Avis Mar 31 '17 at 20:42
13

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

Carlton
  • 5,533
  • 4
  • 54
  • 73
7

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
alsotang
  • 1,520
  • 1
  • 13
  • 15
6

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.

j4v1
  • 1,457
  • 1
  • 22
  • 32
5

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

  • This helped, it removed the branch from my list on intellij and I checked it out as a new branch. Luckily I had pushed my changes so they were all there. – OAM Aug 29 '19 at 11:31
3

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
2

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 :)

1

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 --

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
1

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.

aaronwbrown
  • 113
  • 1
  • 6
1

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

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
1

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!

Ahmet Emrebas
  • 566
  • 6
  • 10
0

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.

Samuelens
  • 156
  • 4
0

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.

vinnief
  • 742
  • 10
  • 13
0

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.

0

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.

Sergio Carneiro
  • 3,726
  • 4
  • 35
  • 51
0

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.

heringer
  • 2,698
  • 1
  • 20
  • 33
0

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

0

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.

drzaus
  • 24,171
  • 16
  • 142
  • 201
0

You can use the below command to resolve your issue

rm .git/refs/remotes/origin/master
git fetch
git pull
Rohit sharma
  • 41
  • 1
  • 11
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 20 '22 at 03:28