62

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.

Jake Miller
  • 2,432
  • 2
  • 24
  • 39

20 Answers20

62

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
VasFou
  • 1,561
  • 1
  • 22
  • 39
  • 17
    Only deleting `.git/refs/heads/branch_name` solved my problem, thanks a lot – Kushal Apr 22 '19 at 09:58
  • this works for me. But the problem now is that it's going to break the chain that connects to the remote repository. therefore you might encounter another error trying to push to the remote, though that can be configured too – PRONO Jul 20 '23 at 13:00
20

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.
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Rafael Marques
  • 211
  • 2
  • 4
12
  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
    
cottontail
  • 10,268
  • 18
  • 50
  • 51
SHAH MD IMRAN HOSSAIN
  • 2,558
  • 2
  • 25
  • 44
6

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

echo ref: refs/heads/master >.git/HEAD
ASCII ALIEN
  • 71
  • 1
  • 2
4

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
stackunderflow
  • 387
  • 4
  • 9
2

I had this problem and I used this command:

git reset
vvvvv
  • 25,404
  • 19
  • 49
  • 81
1

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

vvvvv
  • 25,404
  • 19
  • 49
  • 81
David La Grange
  • 383
  • 1
  • 6
  • 14
1

You want a quick, 100% working solution ?

I tried a few of the ones suggested here (thanks Guys !), but to no fixing the case for me.

  1. rename your directory with _old.

2.git clone your project again, into a brand new directory.

Then , move your files one by one / resume your work.

That's garanteed to work ! - even if playing with git commands looked funnier in the beginning. :)

Note: the spacetime between the "git clone" command and your files will lay on your files only, so beware if you're in the case of several commits and no push done : the commits list (and logic) will be lost.

1

check branch name
git show-ref --head

Firstly, just checked the error, here you will find which reference is broken

Step 1: just remove this mentioned reference
rm .git/refs/heads/branch_name

Step 2: git fetch
Step 3: git pull

Will work 100% sure....

0

This is what fixed my issue:

rm -rf .git/refs/heads/
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Sandip Subedi
  • 1,039
  • 1
  • 14
  • 34
  • 8
    Extremely dangerous if you don't know what you're doing -- this will remove *all* branches from your repository and potentially cause you to lose track of a large number of commits (which will later be deleted if you don't fix it soon). – Soren Bjornstad Oct 18 '20 at 20:55
0

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Taran
  • 2,895
  • 25
  • 22
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • The only trade-off is : all your prev. commits will be lost. – Dwaipayan Chakroborty May 14 '21 at 20:50
  • But, all the changes in files will be tracked, no issues. Just the commit message will be the one you initially commit after initializing a repo, to dodge the error, For me, no commands worked, but this one worked fine. So, if you too are having trouble, follow this one , if commands dont work... – Dwaipayan Chakroborty May 14 '21 at 20:52
  • TL;DR => delete the .git folder/initilaize a repo with a new name, that doesn't already exist in your Github – Dwaipayan Chakroborty May 14 '21 at 20:53
0
  1. Delete your .git folder

git init
git remote add origin url
git commit -m 'msg'
git push origin dev
vvvvv
  • 25,404
  • 19
  • 49
  • 81
  • 10
    You should warn people about what happens when deleting the .git folder, no? I mean what if there's a long history on the repository? You know it will all be lost, right? – Scratte Sep 24 '21 at 19:41
0

remove the head.lock file then retry

rm -rf .git/HEAD.lock 
cottontail
  • 10,268
  • 18
  • 50
  • 51
Anurag
  • 11
  • 1
0

First check the user and group managing your project.

ls -las

enter image description here

Then add permissions

sudo chown -R -c username:group .git/

enter image description here

I hope it helps, good luck.

Sergio Díaz
  • 79
  • 1
  • 7
0

I have the same problem:

1/Go to gitub and copy the id of your last commit Get id of the last commit

2/Use the following command to reset your project on the last commit:

git reset --hard <last commit id on image above>

GhosTHaise
  • 11
  • 2
  • 1
    Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? **If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient.** Can you kindly [edit] your answer to offer an explanation? – Jeremy Caney Aug 28 '23 at 00:46
-1

I have the same problem. I just used command:

git reset

Then I removed file /my_project_directory/./git/refs/heads/master and then I can use this command:

git reset --hard <my_hash_of_last_commit_on_remote_branch>
cottontail
  • 10,268
  • 18
  • 50
  • 51
-1

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Chiaro
  • 1,447
  • 13
  • 13
-1

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

Adeakinwe
  • 1
  • 2
-1

I'm Awesome from Omoyetips. I ran into this issue. My solution was to delete the .git folder, then re-initialize Git. After doing so, it worked.

  • There are two issues with your post. Don't use your posts for self-promotion. Posts like that can be downvoted, flagged, and removed (see [How not to be a spammer](https://stackoverflow.com/help/promotion)). And second, your answer does not provide additional value to the thread, as another user already provided the same solution, with actual commands. If a previous answer was helpful to you, you should [vote it up](https://stackoverflow.com/help/privileges/vote-up) once you have enough [reputation](https://stackoverflow.com/help/whats-reputation). – AlexK Jun 05 '23 at 04:18
  • @AlexK You may be interested in [the moderation strike](https://openletter.mousetail.nl/) – Andreas is moving to Codidact Jun 05 '23 at 04:21