Couldn't set refs/heads/master when commit

39.7k views Asked by At

I was able to commit fine yesterday. But today (I didn't change anything), when I commit:

$ git add config.h
$ git commit -m "Update config.h to reset the values"
error: Couldn't set refs/heads/master
fatal: cannot update HEAD ref

I know that this error may happen also during pull or push. But I haven't found a solution to fix it when committing.

My .git/config file looks like this:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
    hideDotFiles = dotGitOnly
[remote "origin"]
    url = git@SOME_URL
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
7

There are 7 answers

2
Rahul Gupta On BEST ANSWER

It seems you have lost your HEAD, so you will have to recreate it. You can do that using this.

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

This will create a HEAD file in your .git folder. That should solve your problem.

Also, try the git fsck command. It verifies the connectivity and validity of the objects in the database.

git fsck --lost-found

Use this to scan for unreachable objects. It will write dangling objects into .git/lost-found/commit/ or .git/lost-found/other/, depending on type. If the object is a blob, the contents are written into the file, rather than its object name.

1
dmlogs On

Check your .git folder for file HEAD.

$ cat .git/HEAD
ref: refs/heads/master

If this does not exist, create it.

$ echo "ref: refs/Heads/master" > .git/HEAD
0
asdFletcher2 On

I experienced this problem (fatal: couldn't set 'refs/heads/my-branch') when I had 2 copies of VS Code, each with integrated terminals checked out to the same commit. Except one version of VS Code was in WSL (Windows subsystem for Linux) mode. I closed the WSL VS Code and immediately was able to commit in the vanilla non-WSL VS Code.

1
eugen_nw On

For me it took a Windows reboot to get it working again :-)))

0
Kobato On

In my case, in my .git folder, there was file HEAD, and it couldn't rename HEAD.lock to HEAD (even though it didn't tell me). I removed the HEAD file, and it was able to work fine.

(I was also using TortoiseGit; don't know if that had anything to do with it)

2
penno On

This may help others; I waited about a minute and simply retried the commit and ... it worked.

0
Amit Shah On

In my case the issue was with permission on the GIT repository on server for the path refs/Heads/master.

After allowing my username for accessing the mentioned path, all worked well.