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

GitGithubBitbucketSourcetree

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

I had the same problem, this worked for me:

Step 1.

  • go 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.

  • go to .git\refs\heads and open the document named as YOUR_BRANCH delete the line and paste the ID in.

Solution 3 - Git

  1. Navigate to directory .git/refs/heads/branch_name

  2. Delete the preferred branch name

  3. Open terminal(git bash or cmd for windows)

    git reset

  4. Commit the changes (if necessary)

  5. Run the following command for merging the remote repository branch

    git pull
    

    If it gives related to refusing to merge unrelated histories, run the following command in the terminal:

    git pull origin master --allow-unrelated-histories
    

Solution 4 - Git

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

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

Solution 5 - Git

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

Solution 6 - Git

I had this problem and I used this command:

git reset

Solution 7 - Git

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

Solution 8 - Git

This is what fixed my issue:

rm -rf .git/refs/heads/

Solution 9 - Git

  1. Delete your .git folder

git init
git remote add origin url
git commit -m 'msg'
git push origin dev

Solution 10 - Git

Remove the file .git/ORIG_HEAD then pull again. For me the .git/ORIG_HEAD file was 0 bytes and has .lock extension instead of the git reference it was supposed to contain, so I just got rid of it.

Solution 11 - Git

I had the same problem, today. I found a very easy workaround to this problem but has a few trade-offs. I am still not sure what caused it in the first place. I came across a lot of solutions, and unfortunately, nothing really worked for me.

I could not initialize a GitHub repo during this error, which actually helped me find a solution. Apparently, just deleting the .git directory solves a lot of issues. Just when I deleted that directory, I was able to initialize a repo. You shall find the .git folder in your workspace. .git(dir)/config(file) . the config file may be broken and could be the source of the issue. (I am not sure of the cause, any explanations in laymen would be much appreciated)

just when I deleted the folder, all the errors vanished. I was also able to commit from GitHub desktop (which was throwing me the error, previously, the same error while committing directly from the IDE)

The only downside to this would be, all your staged changes might be lost, that is only it. And you may have to initialize a new repo because giving the same name would throw error(if the same repo already exists)

So, apart from that, you shall be good to go. You can commit changes too now.

Solution 12 - Git

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 this command:

git reset --hard <my_hash_of_last_commit_on_remote_branch>

Solution 13 - Git

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.

Solution 14 - Git

Clone the project again, install modules and checkout to your branch. It will be restore the state.

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
QuestionJake MillerView Question on Stackoverflow
Solution 1 - GitVasilisfooView Answer on Stackoverflow
Solution 2 - GitRafael MarquesView Answer on Stackoverflow
Solution 3 - GitSHAH MD IMRAN HOSSAINView Answer on Stackoverflow
Solution 4 - GitASCII ALIENView Answer on Stackoverflow
Solution 5 - GitstackunderflowView Answer on Stackoverflow
Solution 6 - GitAlex RodriguesView Answer on Stackoverflow
Solution 7 - GitDavid La GrangeView Answer on Stackoverflow
Solution 8 - GitSandip SubediView Answer on Stackoverflow
Solution 9 - Gitnaman kumarView Answer on Stackoverflow
Solution 10 - GitTaranView Answer on Stackoverflow
Solution 11 - GitDwaipayan ChakrobortyView Answer on Stackoverflow
Solution 12 - GitKrzysztof WalczewskiView Answer on Stackoverflow
Solution 13 - GitChiaroView Answer on Stackoverflow
Solution 14 - GitAdeakinweView Answer on Stackoverflow