windowsgit

Git on Windows cannot handle refs only differing by case


Every time I try to run git remote remove origin to update it to a new URL, I am getting an error:

error: could not delete references: cannot lock ref 'refs/remotes/origin/foo': Unable to create 'C:/path/to/repo/.git/refs/remotes/origin/foo.lock': File already exists.

It then advises me to try to delete that file manually, but it does not exists, implying that it is only added by git process itself.

This was a complete mystery to me until I run git branch -a and saw that it contains both origin/foo and origin/Foo. So to delete both, git is trying to create foo.lock and Foo.lock, but on Windows they are the same file, so it fails.

How can I resolve this issue? The branches are more than a year old so could be removed, but I don't have permission to do it. I don't want to delete the repo and clone it again to not lose any local work (which I cannot push anywhere due to remote change).


Solution

  • Every time I try to run git remote remove origin to update it to a new URL

    That's overkill. Do git remote set-url origin or git config remote.origin.url.

    The branches are more than a year old so could be removed, but I don't have permission to do it.

    Contact the repository owner and have them remove one of the branches.

    It then advises me to try to delete that file manually

    Delete them one by one, using git branch -r -d or git update-ref -d (the former takes just origin/test, the latter takes the full refs/remotes/ name).

    If that fails: Delete the actual ref file manually, not the .lock file. Your clone most likely isn't yet using the new "reftable" DB format, so there are two places that local refs may exist in:

    "Remote-tracking branch" refs are refs/remotes/origin/* as the message says. You can delete them by 1) manually deleting the corresponding .git/refs/remotes/origin/ file and 2) carefully editing .git/packed-refs to delete just the unwanted lines.