Git fatal: cannot lock ref

Sometimes this can happen if the name of the branch you create doesn't match git naming conventions or those set by you projects git administrator. Changing the name to one that does, can can fix this issue.

This worked for me :

git --no-optional-locks fetch --prune origin 

I was looking for answer here, but actually my problem was simpler, yet unresolvable.

  • fresh clone of repo
  • git checkout foo/bar
  • git checkout -b foo/bar/baz
  • got similiar error message.

As described here, you cannot use foo/bar both as a branch and directory. I had to change proposed naming convention.

It looks like the problem here was a branch X existed locally, and you tried to create a new branch X/Y. This won't work, because there will be a file at .git/refs/heads/X, so git can't also create the folder X.

However, it's worth adding that including "origin" in your branch name points to some confusion - origin is usually the name of the remote repository. If you run git branch I expect you will see local branches with origin in the name.

The format git checkout X is shorthand for:

  • look for a local branch X and check that out if it exists
  • otherwise look for a remote branch X and check that out locally (git checkout -b X origin/X)

To fix your current state, you can likely do this (see here):

git update-ref -d refs/heads/origin/branch