Search code examples
gitgithubbitbucketbitbucket-pipelines

Syncing Bitbucket to Github not Bringing over All Branches


I'm trying to sync our repository on Bitbucket with a fresh repository on Github, such that when I push code to origin (Bitbucket), it pushes that commit on to the "mirrored" Github repository.

To do this, I created the Github repo and set up the ssh keys etc. I then added a Pipleline to Bitbucket called bitbucket-pipelines.yml which has the following code:

clone:
  depth: full
pipelines:
  default:
    - step:
        script:
          - git push --mirror git@github.com:orgname/nameofrepo.git

This brought over every commit and tag and the branch which I was currently on, but it did not bring over the other branches.

I suspect it has something to do with the fact that they all start with the word origin, but that is just a theory based on the fact that the only branch which did come across did not start with origin.

I've also tried a variation where I use:

      - step:
          clone:
            depth: full # want all so can push all (maybe can optimise this in future?)
          name: 'Sync push on branch to github'
          script:
            - git remote add sync git@github.com:orgname/nameofrepo.git
            - git push sync --all --force
            - git push sync --tags --force

Exact same result.

This is what other people (on blogs etc) have been doing to achieve this and I'm assuming they are trying to sync more than just main.

Can anyone spot what I am doing wrong?


Solution

  • For you push --mirror to push all branches, you might need first, in your Bitbucket pipeline, to add a step where you make a local branch out of all the remote tracking origin/xxx branches.

    I proposed a one-liner before to do that.

    for i in $(git for-each-ref --format=%(refname:short) \
      --no-merged=origin/HEAD refs/remotes/origin); do \
        git switch --track $i; \
    done
    

    Once your Bitbucket has cloned and created all local branch, you can proceed with a push --mirror.

    Note: that would necessitate a Bitbucket pipeline using Docker images as build environments, running a default Docker image.
    That would include a recent Git 2.39.1.
    The OP confirm in the chat using a build environment with... Git 1.9.1 (March 2014).

    Using the default Atlassian image solves this, as it comes with the latest Git 2.39.1

    image: atlassian/default-image:4.20230131
    clone:
      depth: full
    pipelines:
      default:
        - step:
          script:
          - 'for i in $(git for-each-ref --format="%(refname:short)" --no-merged=origin/HEAD refs/remotes/origin); do echo $i; git show-ref --verify --quiet refs/heads/${i#origin/} || git switch --track $i; git branch -l; done; git branch -lvv; git push --mirror git@github.com:you/repo.git'
    

    The script in multiple lines:

    for i in $(git for-each-ref --format="%(refname:short)" \
                    --no-merged=origin/HEAD refs/remotes/origin); do 
      echo $i; 
      git show-ref --verify --quiet refs/heads/${i#origin/} || git switch --track $i; 
      git branch -l; 
    done; 
    git branch -lvv; 
    git push --mirror git@github.com:you/repo.git