Git cannot lock ref 'HEAD': unable to resolve reference HEAD

人走茶凉 提交于 2019-12-19 14:16:19

问题


I'm trying to commit the changes to my repository but I receive the error below:

git -c diff.mnemonicprefix=false -c core.quotepath=false commit -q -F C:\Users\Contronym\AppData\Local\Temp\bkdweixb.mnu
fatal: cannot lock ref 'HEAD': unable to resolve reference HEAD: Invalid argument

Completed with errors, see above.

I'm using bitbucket and SourceTree.

What's the reason for this commit failing? I was able to commit just fine the last 3 commits over the past week. Then, all of a sudden, I receive this error.

EDIT

I ran git gc and these are the results:

$ git gc
error: bad ref for HEAD
error: bad ref for HEAD
error: inflate: data stream error (unknown compression method)
fatal: loose object 53b65bd9b4fec7f6a7b0b3313c68199a18804327 (stored in .git/objects/53/b65bd9b4fec7f6a7b0b3313c68199a18804327) is corrupt
error: failed to run repack

I checked the directory .git/objects/53/b65bd9b4fec7f6a7b0b3313c68199a18804327 but that doesn't exist. There's two other files there, but b65bd9b4fec7f6a7b0b3313c68199a18804327 doesn't exist.


回答1:


I had the same problem and the only solution that I found was to navigate to the head like so :

.git/refs/heads/branch_name

And I deleted the head file . Then I went to the console and I used the command :

git reset

Then all the files were unstaged so add them and commit them afterwards.




回答2:


I had the same problem, this worked for me:

Step 1) - got to .git\logs\refs\heads and open the Document named as YOUR_BRANCH, now copy the ID -numbers in front of your user name and email-

Step 2) - got to .git\refs\heads and open the document named as YOUR_BRANCH delete the line and paste the ID in.

Hope it works for you to




回答3:


Worked for me, into terminal enter: (branch accordingly to your desires lul)

echo ref: refs/heads/master >.git/HEAD



回答4:


If you don't mind losing your history, you can delete the .git file and then

git init

This will reinitialize your repository and you can then proceed from there




回答5:


I had the same issue after calling git commands with the root user inside the working copy. So the owner and owner group of various files under .git/ were changed to "root".

When I switched back to my user account, git could not handle this files anymore, because of the lacking permissions.

It worked again, after resetting the permissions with

sudo chown -R [MY_USER]:[MY_GROUP] .git



回答6:


When I run into this I just git clone the project into a new file directory and pull the heads folder from located at .git\refs\heads and replace the original heads file in the directory that your having the problem. Then just delete the new clone you created (since it obviously doesn't have the updates your trying to push).




回答7:


I have the same problem. I just used command:

git reset

Than I removed file /my_project_directory/./git/refs/heads/master and than I can use command

git reset --hard <my_hash_of_last_commit_on_remote_branch>


来源:https://stackoverflow.com/questions/39057962/git-cannot-lock-ref-head-unable-to-resolve-reference-head

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!