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.


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.

Tags:

Git

Git Commit