Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Mirroring Repo with Github Only Pushes 1 Branch

David Rogers February 16, 2023

Hi There

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.

Do you know how to achieve this?
Thanks

2 answers

1 accepted

0 votes
Answer accepted
David Rogers February 19, 2023

OK. I worked through this and will leave the answer here for the community.
The first step is to get an updated version of GIT.

As pipelines are run in docker containers, you just need to specify an updated image. (The images are on Docker Hub). That's the 1st line in my yml below.

The clone only creates/tracks the branch which you are currently on. So, you need to track all the other branches. You can also filter out branches which were already merged.

You do this with a for-each-ref statement (a git thing). Then, handle master and develop separately, because for some reason, the --no-merged flag excludes them.

Then, do the mirror statement as before.


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; done; git switch --track origin/master; git switch --track origin/develop; git push --mirror git@github.com:nameOfYourOrganization/nameOfYourRepository.git'         

 

And that will push all your branches to Github.

Vladi Iancu
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 3, 2023

This works for pipelines running on all commits on all branches, except the master one.

So if you do a PR the pipeline will do it's thing and pass. 

But once you merge the PR and the pipeline runs agains (this time it will git clone --branch="master") the pipeline fails when trying to git switch to master.

0 votes
David Rogers February 18, 2023

na

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events