问题
在 Jenkins 上通过流水线尝试拉取代码的时候,Git 返回如下错误:
current Git branch is HEAD detached at 0ff123e
解决方法
当 Jenkins 的 Git 插件 clone 一个 repo 的时候,它会 check out 出一个特定的commit,而不是 repo 的 HEAD,这会导致 repo 处于“detached”状态,因此如果你想对 repo 执行进一步的 git 操作,就需要在 shell 脚本中执行下面的命令来指定分支名:
git checkout ${BRANCH_NAME}
参考:
git checkout-- detach Rather than checking out a branch to work on it, check out a commit for inspection and discardable experiments. This is the default behavior of "git checkout <commit>" when <commit> is not a branch name. See the "DETACHED HEAD" section below for details.