如何配置Azure DevOps仅向CodeCommit推送指定分支
报错原因与解决方案
你使用git clone --mirror拉取的仓库会默认在本地配置里开启remote.origin.mirror = true,该配置要求push时必须全量同步所有引用,不能同时指定单独的分支(refspec)推送,这就是你遇到fatal: --mirror can't be combined with refspecs报错的根本原因。
另外你修改后的推送命令语法也有问题,git push的参数顺序应该是「远程地址/远程名称」在前,「分支规则」在后,你把origin develop写在目标仓库地址前面属于语法错误,即使没有mirror配置也无法正常运行。
方案1:使用轻量单分支克隆(推荐)
你只需要同步develop分支,无需全量克隆整个镜像,该方案拉取速度更快,占用资源更少,修改后的脚本如下:
$env:GIT_REDIRECT_STDERR = '2>&1' # 仅拉取develop分支 git clone --single-branch --branch develop https://$env:SYSTEM_ACCESSTOKEN@dev.azure.com/MelonElevenPoint2/ThrottleUp/_git/ThrottleUp_Mobile cd ThrottleUp_Mobile # 强制推送本地develop分支到CodeCommit的develop分支 git push --force https://$(user):$(pass)@git-codecommit.us-east-2.amazonaws.com/v1/repos/ThrottleUp develop:develop
方案2:保留镜像克隆,推送时禁用mirror模式
如果你有其他场景需要保留--mirror克隆的逻辑,只需要修改推送步骤,添加--no-mirror参数禁用镜像同步规则即可:
$env:GIT_REDIRECT_STDERR = '2>&1' git clone --mirror https://$env:SYSTEM_ACCESSTOKEN@dev.azure.com/MelonElevenPoint2/ThrottleUp/_git/ThrottleUp_Mobile cd ThrottleUp_Mobile.git # 禁用mirror模式,单独推送develop分支 git push --no-mirror --force https://$(user):$(pass)@git-codecommit.us-east-2.amazonaws.com/v1/repos/ThrottleUp refs/heads/develop:refs/heads/develop
内容的提问来源于stack exchange,提问作者Васко Бошков
相关产品推荐
相关产品推荐

