Jake Miller
Jake Miller

Reputation: 2652

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

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.

Upvotes: 74

Views: 160195

Answers (22)

On most OS's, that .git folder is hidden from plain sight.

Windows: Check Hidden Items first, only then the .git folder will be visible.

Then you can go ahead and delete the folder refs/heads/master file and then do git reset.

Upvotes: 1

Defria Manda
Defria Manda

Reputation: 86

Here is what worked for me when I encountered this error.

error: update_ref failed for ref 'ORIG_HEAD': cannot lock ref 'ORIG_HEAD': unable to resolve reference 'ORIG_HEAD': reference broken

rm -rf .git/ORIG_HEAD
git reset

Upvotes: 1

ShellDude
ShellDude

Reputation: 606

There's an even easier way to address this outside of having to reset your local repo / etc.

Within the .git folder is a specific file you should be looking for and there is another within your .git/refs/heads directory you should have as well.

Do you see HEAD.lock in .git/? Delete it or rename it to something like Head.lock.save if you're paranoid.

Do you see a {branch_name}.lock file in .git/refs/heads too? {branch_name} should be the name of the repo branch you are actively trying to commit to. Delete / rename it too.

Now retry your commit and you should be good to go. There is no need to wipe out entire directories and reset your local repo to recover from this.

Upvotes: 0

GhosTHaise
GhosTHaise

Reputation: 39

I have the same problem:

1/You need to remove all local heads store on your machine

->Remove .git/refs/heads/

->Remove .git/refs/remotes/origin/

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

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

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

Upvotes: 0

Sergio D&#237;az
Sergio D&#237;az

Reputation: 89

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.

Upvotes: 0

sonali lotankar
sonali lotankar

Reputation: 31

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

Upvotes: 3

SHAH MD IMRAN HOSSAIN
SHAH MD IMRAN HOSSAIN

Reputation: 2899

  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
    

Upvotes: 14

Krzysztof Walczewski
Krzysztof Walczewski

Reputation: 670

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>

Upvotes: -1

Anurag
Anurag

Reputation: 31

remove the head.lock file then retry

rm -rf .git/HEAD.lock 

Upvotes: 2

Bastien Gallienne
Bastien Gallienne

Reputation: 168

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.

Upvotes: 1

Chiaro
Chiaro

Reputation: 1537

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.

Upvotes: -2

naman kumar
naman kumar

Reputation: 5

  1. Delete your .git folder

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

Upvotes: -2

Taran
Taran

Reputation: 3231

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.

Upvotes: 2

Sandip Subedi
Sandip Subedi

Reputation: 1077

This is what fixed my issue:

rm -rf .git/refs/heads/

Upvotes: 0

David La Grange
David La Grange

Reputation: 413

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

Upvotes: 1

Alex Rodrigues
Alex Rodrigues

Reputation: 21

I had this problem and I used this command:

git reset

Upvotes: 2

Rafael Marques
Rafael Marques

Reputation: 241

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.

Upvotes: 23

VasFou
VasFou

Reputation: 1669

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.

Upvotes: 71

Adeakinwe
Adeakinwe

Reputation: 1

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

Upvotes: -1

Dwaipayan Chakroborty
Dwaipayan Chakroborty

Reputation: 302

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.

Upvotes: 0

stackunderflow
stackunderflow

Reputation: 436

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

Upvotes: 4

ASCII ALIEN
ASCII ALIEN

Reputation: 81

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

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

Upvotes: 7

Related Questions