问题
Windows 用户经常会遇到这个问题
git pull
给出错误:error: cannot lock refunable to update local ref
原因
原因 a) 有多个分支,其
从开头到任何斜线(或到结尾)的名称,仅大小写不同。
Branch name clashing (upper/lower case)
#######################################
# Example 1)
#############################
feature/releasecandidate/fix123
feature/releaseCandidate/improveFeature789
------------------------
^
Identical from beginning up to a slash (here the 2nd one)
except for the marked letter, where the upper/lower case differs
# Example 2)
#############################
releaseBranch
releasebranch
-------------
^
Identical from beginning to the end
except for the marked letter
原因 b) 在 linux 上也是一个问题:一个分支是另一个分支的前缀,带有斜线边界:
Prefix with slash-boundary
#######################################
# Example 1) - also a problem on linux
#############################
feature/release2021
feature/release2021/fixIssue07
^
slash boundary
# Example 2)
#############################
feature/release2022
feature/Release2022/fixIssue99
^ ^
differing case slash boundary
(problem on
windows)
解决方案
消除原因(具体参见上面的原因)。
# inspect your branches, to see if you have the upper/lower case problem
git ls-remote --heads YOUR-GIT-URL
例如:创建一个分支命名策略,例如全部用小写字母;或最后一个斜线之前的小写字母。或一些检测违规行为的智能钩子。 (但请注意:在原因 a)问题仅在 windows 上,而不是在 linux 上)。
背景
问题是 Windows 将这些分支(来自示例 1 和 2)存储在 .git 文件夹中
# inspect the files/folders under
.git/refs/remotes/origin/
并且在Cause a)窗口无法区分
大写/小写的差异,所以 git on window 变得疯狂。
在原因 b) 中,您不能拥有与文件 (feature/release2021) 同名的文件夹(例如 feature/release2021/)。
解决方法
通常有效的短期解决方法(直到您消除原因)是:
git pack-refs --all
# delete the contents of .git/refs/remotes/origin/*
rm -rf .git/refs/remotes/origin/*
git pull; git pull; git pull # all good? yes!