使用Maven Release插件时Azure Pipeline报错:pathspec 'main'未匹配Git文件
解决Azure发布管道中Maven Release插件的"git checkout main"错误
错误现象
在后台运行搭载Maven Release插件的Azure发布管道时,执行git checkout main脚本触发如下错误:
Starting: Reattach detached head ============================================================================== Task : Bash Description : Run a Bash script on macOS, Linux, or Windows Version : 3.227.0 Author : Microsoft Corporation Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash ============================================================================== Generating script. Script contents: git checkout main ========================== Starting Command Output =========================== /usr/bin/bash /agent/_work/_temp/b44c1c2b-b722-4d90-a662-570bb11df3cb.sh error: pathspec 'main' did not match any file(s) known to git ##[error]Bash exited with code '1'. Finishing: Reattach detached head
解决方案
确认分支名称准确性
部分仓库默认分支仍为master而非main,先在管道中添加git branch -a命令查看本地及远程所有分支,确认目标分支名称后,将脚本中的main替换为实际分支名。确保克隆时拉取全部分支
Azure管道的Git克隆任务默认仅拉取单个分支,需在克隆步骤中启用"Checkout all branches"选项;或在git checkout命令前执行git fetch --all,确保本地仓库能识别远程的main分支。正确修复分离头状态
若Maven Release插件执行后导致Git处于分离头状态,不要直接执行git checkout main:- 先通过
git log查看最近的提交哈希值; - 若本地未创建
main分支,执行git checkout -b main <提交哈希>创建并切换分支; - 若
main分支已存在,先执行git fetch origin main拉取远程分支到本地,再执行git checkout main。
- 先通过
内容的提问来源于stack exchange,提问作者Splioo
相关产品推荐
相关产品推荐

