Couldn't set refs/heads/master when commit
Asked Answered
J

8

19

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
Jade answered 12/6, 2015 at 17:43 Comment(0)
P
18

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.

Pinnatifid answered 12/6, 2015 at 17:51 Comment(2)
Thanks. I've tried it but this doesn't solve the problem. I have the HEAD fileJade
"It seems you have lost your HEAD" statement of the day ;)Deering
P
11

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.

Pennyroyal answered 30/9, 2020 at 23:15 Comment(0)
P
5

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

Parisparish answered 25/7, 2016 at 22:53 Comment(2)
Worked for me too. I was rebasing on the command line and eclipse had something locked. Closed eclipse and git rebase --continue worked.Fulvia
I think for me it was Dropbox; this solution worked for me as well.Freezedrying
S
2

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.

Schoonmaker answered 18/9, 2019 at 8:45 Comment(0)
H
2

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

Houser answered 2/2, 2021 at 20:21 Comment(1)
same, tried lot of things but they didn't work until I restarted WindowsSmall
L
1

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
Lussi answered 12/6, 2015 at 17:51 Comment(1)
Thanks. I've tried it but this doesn't solve the problem. I do have the .git/HEAD fileJade
R
0

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)

Ramie answered 13/9, 2021 at 0:1 Comment(0)
T
0
  1. Shelve yours changes;
  2. Delete local branch;
  3. Checkout branch again;
  4. Unshelve the changes;
  5. Commit and Push yours changes.
Timberwork answered 13/8 at 4:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.