git repository mirror with detached head causes error: GH002

git repository mirror with detached head causes error: GH002

以下代码 运行 在 Azure Pipeline 中,用于将源镜像到另一个 git 存储库。

git remote add --mirror=fetch mirror MIRROR_REPO

git push mirror --progress --prune +refs/remotes/origin/*:refs/heads/* +refs/tags/*:refs/tags/*

当代理运行时,它会检查特定提交处的代码,这会导致分离的头具有提交 SHA 的名称。这是代理生成的日志:

 * [new ref]           6ADFB183A4A2C94A2F92DAB5ADE762A47889A5A1 -> origin/6ADFB183A4A2C94A2F92DAB5ADE762A47889A5A1
git checkout --progress --force 6ADFB183A4A2C94A2F92DAB5ADE762A47889A5A1
Note: switching to '6ADFB183A4A2C94A2F92DAB5ADE762A47889A5A1'

我想这就是 git push mirror 命令失败并出现以下错误的原因:

remote: error: GH002: Sorry, branch or tag names consisting of 40 hex characters are not allowed.        
remote: error: Invalid branch or tag name "6ADFB183A4A2C94A2F92DAB5ADE762A47889A5A1"   

我正在寻找一种方法,可以在每次管道运行时将所有分支和标签从我的源存储库推送到我的镜像存储库。

谢谢!

remote: error: GH002: Sorry, branch or tag names consisting of 40 hex characters are not allowed.

根据我的测试,我可以重现同样的问题。

在 azure devops 默认签出步骤中,它会将 commitid 设置为 [New Ref]。由于这是默认步骤,我们无法干预它的工作方式。

对于解决方法,您可以跳过默认的签出步骤 (- checkout: none) 和 运行 git 命令来签出所有分支和标签。

这是我的示例:

steps:
- checkout: none

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      git clone --mirror  sourcerepo

      cd $(build.sourcesdirectory)\reponame
      
      git remote add --mirror=fetch mirror MIRROR_REPO
      
      git push mirror --progress --prune +refs/heads/*:refs/heads/* +refs/tags/*:refs/tags/*

然后它将所有分支和标签成功推送到Github。

这不会发生,因为分支名称很奇怪,正如评论中提到的那样。 它也发生在更传统的名字上。 该错误似乎和它所说的一样奇怪:分支名称不能恰好有 40 个字符。它们可以更短或更长。

这不是这个特定问题的答案,因为分支名称是自动创建的。 但是其他用户(比如我)可能会因为同样的错误而进入这个页面。 对我们来说,解决方法是让分支名称更长或更短一个字符。