如何配置Git避免git pull时递归拉取子模块?
问题背景
在WSL(Ubuntu 22.04)环境下,使用Git 2.34.1操作包含子模块的仓库时,首次执行git pull会自动递归拉取子模块且失败,再次执行则仅更新主仓库。当前git config -l未显示submodule.recurse或fetch.recursesubmodules配置项。
首次执行git pull的错误输出:
user@computer:/path/repo$ git pull
remote: Enumerating objects: 5254, done.
remote: Counting objects: 100% (2042/2042), done.
remote: Compressing objects: 100% (741/741), done.
remote: Total 1276 (delta 737), reused 893 (delta 483), pack-reused 0 (from 0)
Receiving objects: 100% (1276/1276), 2.45 MiB | 4.85 MiB/s, done.
Resolving deltas: 100% (737/737), completed with 361 local objects.
From url:repo
[update list]
Fetching submodule [submodule name]
fatal: git upload-pack: not our ref [commit hash]
fatal: remote error: upload-pack: not our ref [commit hash]
Errors during submodule fetch:
[submodule name]
仓库的.gitmodules配置:
[submodule "name1"] path = submod1 url = ../submod1.git branch = dev [submodule "name2"] path = submod2 url = ../submod2.git branch = dev [submodule "name3"] path = submod3 url = ../submod3-different-name.git branch = dev
解决方案
要让git pull不再递归拉取子模块,可通过Git配置强制禁用该行为,有两种方式可选:
1. 仅当前仓库生效(推荐)
在仓库根目录执行以下命令:
git config --local pull.recurseSubmodules no
2. 全局生效(所有仓库)
如果希望所有仓库的git pull都不递归拉取子模块,执行:
git config --global pull.recurseSubmodules no
验证配置
执行以下命令确认配置已生效:
git config -l | grep recurseSubmodules
输出应包含pull.recursesubmodules=no(Git配置项大小写不敏感)。
内容的提问来源于stack exchange,提问作者Jeff G

