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
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 is 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 the most used Git commands, check this post.
Comments
I got this same error while doing a ‘git fetch’ on my branch that I was on, in Windows, but using git bash (not sure if that matters).
To fix it I used the following command:
$ git pack-refs –all
After that my ‘git fetch’ worked.
AFAIK this will create a hash in a git file (somewhere) and the lookup will use that instead of the case-sensitive branch name. It’s a work-around and a known git bug. If the problem re-appears you just run that ‘git pack-refs –all’ command again.
Thank you for the info. I didn’t know the command.
I checked the official documentation and it should work.