Git子模块可拉取但无法推送至Bitbucket的问题求助
问题背景
Bitbucket上主仓库mainrepo包含子模块mainrepo/modules/mymodule,子模块的.git目录位于mainrepo/.git/modules/mymodule。主仓库与子模块均有develop(已存在于远程)、feature(本地新建未推远程)分支。本地已完成feature分支的提交与origin/develop的合并,执行git push --set-upstream origin feature时主仓库推送成功,但子模块报错:
The requested repository either does not exist or you do not have access. If you believe this repository exists and you have access, make sure you're authenticated.
fatal: Could not read from remote repository.Please make sure you have the correct access rights
and the repository exists.
子模块可正常执行git pull,但无法推送,重新配置SSH密钥后问题仍存在。
排查与解决步骤
1. 检查子模块远程URL协议
子模块能拉取但无法推送,可能是远程URL使用了只读协议(如HTTPS),或URL配置错误:
- 进入子模块目录:
cd mainrepo/modules/mymodule - 查看当前远程URL:
git remote -v - 将HTTPS协议改为Bitbucket SSH格式(替换为你的用户名和仓库名):
git remote set-url origin git@bitbucket.org:your-username/mymodule.git - 重新尝试推送:
git push --set-upstream origin feature
2. 确认子模块.git目录权限
子模块的.git目录位于主仓库的.git/modules下,可能权限不足导致推送失败:
- 返回主仓库根目录:
cd mainrepo - 检查权限:
ls -la .git/modules/mymodule - 调整当前用户读写权限:
chmod -R u+rw .git/modules/mymodule
3. 验证SSH密钥的写入权限
Bitbucket的Access Key可能仅配置了只读权限:
- 登录Bitbucket,进入子模块仓库的Settings > Access keys
- 确认你的SSH公钥权限为Write,若为只读则修改权限或重新添加带写入权限的密钥
4. 手动进入子模块推送
跳过主仓库的子模块触发逻辑,直接在子模块目录操作:
- 进入子模块目录:
cd mainrepo/modules/mymodule - 切换到feature分支:
git checkout feature - 手动推送并关联上游:
git push --set-upstream origin feature
5. 检查子模块分支跟踪状态
子模块的feature分支可能未正确关联远程分支:
- 在子模块目录查看分支跟踪信息:
git branch -vv - 若未关联,手动设置后推送:
git branch --set-upstream-to=origin/feature featuregit push
内容的提问来源于stack exchange,提问作者francler

