Reputation: 6987
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
Upvotes: 104
Views: 58918
Reputation: 41
You can use the below command to resolve your issue
rm .git/refs/remotes/origin/master
git fetch
git pull
Upvotes: 0
Reputation: 944
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!
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!
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!
Upvotes: 1
Reputation: 25034
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.
Upvotes: 0
Reputation: 41
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
Upvotes: 0
Reputation: 3208
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.
Upvotes: 0
Reputation: 3966
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.
Upvotes: 0
Reputation: 17384
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
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.
Upvotes: 1
Reputation: 9
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.
Upvotes: 0
Reputation: 51
i managed to solve this with deleting master file in git\refs\heads directory
Upvotes: 5
Reputation: 1597
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.
Upvotes: 6
Reputation: 31
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
Upvotes: 3
Reputation: 123
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.
Upvotes: 1
Reputation: 31
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:
git checkout master
git reset --hard
So even when you make a dumb mistake, you're not immediately taken back by a whole day of work with git :)
Upvotes: 2
Reputation: 18931
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 --
Upvotes: 1
Reputation: 821
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.
Upvotes: 0
Reputation: 1980
I managed to recover through:
rm .git/refs/remotes/origin/HEAD
git fetch --all
Upvotes: 183
Reputation: 156
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.
Upvotes: 0
Reputation: 5731
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
<problem-branch>
filegit fetch --all
to get the branch againThen the tab auto completion started working again
Upvotes: 13
Reputation: 1605
If there is not many modified files, I think the convient way to solve this problem is:
git commit -a
Upvotes: 7
Reputation: 13653
Start by following the steps suggested in Recovering broken git repository:
.git/refs
still contains anything usefulgit reflog
and failing that the contents of .git/logs/refs/heads/master
or whatever branch you were on lastgit 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
.
Upvotes: 27