Couldn't set refs/heads/master when commit

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.

  • LibGitSharp: Checkout Remote Branch
  • Git non-fast-forward updates were rejected Merge the remote changes
  • Remove files from local git but not from remote
  • How to resolve git's “not something we can merge” error
  • How did my coworker manage to make git checkout move files?
  • New git repository error on first push
  • My .git/config file looks like this:

        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

  • Unsupported Git version
  • Prevent Git from changing permissions on pull
  • combine two bash commands into a one shortcut prompt
  • Why some git commands are not allowed in the .git directory?
  • VSO Build definition to retrieve code from multiple repositories and multi-step build and reference
  • Is it ok to have the git repository in web root for live site?
  • 3 Solutions collect form web for “Couldn't set refs/heads/master when commit”

    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.

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

    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
    Git Baby is a git and github fan, let's start git clone.