51

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,244
  • 2
  • 21
  • 38

14 Answers14

51

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.

vinzee
  • 17,022
  • 14
  • 42
  • 60
Vasilisfoo
  • 1,435
  • 1
  • 21
  • 39
17

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.
vinzee
  • 17,022
  • 14
  • 42
  • 60
Rafael Marques
  • 181
  • 1
  • 4
  • This almost worked.. I had do to similar for the refs\remote\origin\YOUR_BRANC that file was also full of garbage. To clarefy: the log-file i.e. (.git\logs\refs\head\master) contains the reference number to and from for your commits. In your .git\refs\heads\master you put the last to-id. Then in your .git\refs\remote\master you put a little older number (depending on when you did you last sync) Then you can sync any commit between those numbers. – Svakinn May 26 '20 at 14:42
  • 2
    this was what I needed. My file got corrupted after pushing exactly when a blackout started and all other solutions had me removing my local changes. small clarification: there's a history log with many commit ids under `logs\refs\heads`, make sure to choose the id for the commit you need! – Panos Gr Aug 06 '21 at 12:48
  • Wow man!! This should be the accepted answer! In my case I had 10's of commit, as you explained, I copied the last commit ID to the file and it instantly showed the branch name in Visual studio! – Chandraprakash Nov 20 '21 at 01:33
  • You really saved my day, Thanks for the quality solution. I think this is the only solution in this entire answer list which provides option to restore the corrupted branch (due to power shutdown etc), rather than deleting the branch like others suggest – Chandraprakash Nov 20 '21 at 01:34
10
  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
    
vinzee
  • 17,022
  • 14
  • 42
  • 60
SHAH MD IMRAN HOSSAIN
  • 2,096
  • 2
  • 19
  • 38
  • 1
    Thanks, I had to delete the entire `.git` folder, rerun `git init` and add remote, and then the `--allow-unrelated-histories` flag for `git pull` saved me :) – David Callanan Dec 20 '20 at 23:06
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
3

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
  • 317
  • 3
  • 9
2

I had this problem and I used this command:

git reset
vinzee
  • 17,022
  • 14
  • 42
  • 60
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).

vinzee
  • 17,022
  • 14
  • 42
  • 60
David La Grange
  • 343
  • 1
  • 5
  • 12
1

This is what fixed my issue:

rm -rf .git/refs/heads/
vinzee
  • 17,022
  • 14
  • 42
  • 60
Sandip Subedi
  • 907
  • 1
  • 12
  • 31
  • 7
    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
1
  1. Delete your .git folder

git init
git remote add origin url
git commit -m 'msg'
git push origin dev
vinzee
  • 17,022
  • 14
  • 42
  • 60
  • 6
    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 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.

vinzee
  • 17,022
  • 14
  • 42
  • 60
Taran
  • 2,387
  • 23
  • 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
  • 26,923
  • 21
  • 73
  • 125
  • 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
-1

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>
vinzee
  • 17,022
  • 14
  • 42
  • 60
-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.

vinzee
  • 17,022
  • 14
  • 42
  • 60
Chiaro
  • 1,353
  • 11
  • 11
-1

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

Adeakinwe
  • 1
  • 2