gitgithubazure-devopsazure-pipelinesmirror

git repository mirror with detached head causes error: GH002


The following code is running in an Azure Pipeline to mirror the source to another git repository.

git remote add --mirror=fetch mirror MIRROR_REPO

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

When the agent runs it checks out the code at a specific commit which results in a detached head which has the name of the commit SHA. This are logs produced by the agent:

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

I guess that's the reason the git push mirror command fails with the following error:

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

I'm looking for a method to push all branches and tags from my source repository to my mirror repository every time the pipeline runs.

Thanks!


Solution

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

    Based on my test, I could reproduce the same issue.

    In azure devops default checkout step, it will set the commitid as the [New Ref]. Since this is the default step, we cannot interfere with how it works.

    For a workaround, you could skip the default checkout step(- checkout: none) and run the git command to checkout all branches and tags.

    Here is my sample:

    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/*
    

    Then it will push all branches and tags to Github successfully.