Git “error: cannot lock ref” error

Other techs

The following error occurred in my environment when I executed git pull.

error: cannot lock ref 'refs/remotes/origin/branch-name': is at <commit-number> but expected <different-commit-number>
From <git-repository-path> ! <commit-number>...<commit-number> <branch-name> -> <origin/branch-name> (unable to update local ref)
error: Could not fetch origin
Sponsored links

What causes this error

If a branch name includes a slash, Git tries to create a folder. For example, if the branch name is bugfix/branch-name1, we can find the file your-directory/.git/refs/remotes/origin/bugfix/branch-name1.
I’ve not tested it but I guess if we create another branch Bugfix/branch-name2, we can find the file your-directory/.git/refs/remotes/origin/bugfix/branch-name2. The second branch name starts with an upper case while the first one is the lower case but Git can create the file because the file name is different.

However, if there are bugfix/branch-name1 and Bugfix/branch-name1 in the repository, Git throws this error. It seems that Git is case sensitive but windows not. It’s impossible to create two folders that have the same name but different cases like bugfix and BUGFIX.

bugfix/branch-name1/branch1 causes this error if bugfix/branch-name1 has already existed.

Check if there are two branches that have the same name

Check if there are two branches that have the same name with this command.

git branch -a

If the branch is found, remove one of them or rename them.

Update local repository

Once the branch is removed, we need to update the local repository. Otherwise, the branch still remains in our repository. We need to remove branches that are no longer in the remote repository. Execute the following command. It removes all branches from the local repository that have gone from the remote repository.

git fetch --prune

Try to execute git pull or git fetch. It should work now.

If you want to learn most used Git commands, check this post.

Comments

Copied title and URL