Skip to content

Instantly share code, notes, and snippets.



Forked from MoOx/
Created May 21, 2018
What would you like to do?
How to keep in sync your Git repos on GitHub, GitLab & Bitbucket easily
title tags authors
How to keep in sync your Git repos on GitHub, GitLab & Bitbucket easily

GitHub is nice, but maybe, just in case of long outage or because you don’t want to be tied to GitHub that much, you may want to have mirrors or your repos somewhere else. Here is a nice way to keep not just read only mirrors, but real git repos on GitLab and BitBuckets as well.

Reminder: to be safe setup SSH and Two factor Auth for all places (except Two factor Auth for BitBuckets, cause it’s not compatible with the CLI tool).


Git Tooling

In order to facilitate the setup, we will install some CLI tools for each services.


We will use hub.

For macOS, it’s easy.

brew install hub

See Hub installation instruction page for others OSes.

You will need a GitHub token.

Place it in your home folder in a .github_token file, and load it in your .bash/zshrc like this:

if [[ -f $HOME/.github_token ]]
  export GITHUB_TOKEN=$(cat $HOME/.github_token)


GitLab CLI is available via rubygem:

(sudo) gem install gitlab

Please set an endpoint to API

GitLab requires a token and an endpoint.

For the token, grab you GitLab private token here and use the same solution as GitHub. Here is an example using GitLab “official” online instance that you should add in your .bash/zshrc:

if [[ -f $HOME/.gitlab_token ]]
  export GITLAB_API_PRIVATE_TOKEN=$(cat $HOME/.gitlab_token)


BitBucket CLI is available via pip:

(sudo) pip install bitbucket-cli

BitBucket does not work well with a token… 2fa is not convenient (and impossible to use with ssh) So you will have to enter login/pwd all the time or put that in clear in a .bitbucket file

Now that we have all the tools, let's start by creating a repo on each services.

Create a repo on GitHub, GitLab & Bitbucket using CLI

The commands below assume that your username is the same on each services. If that's not the case, just adjust the command by replacing all variables.

We will create/reuse a folder, init a local git repo, and push it to all those services.

Your git repo exists

Just go into your repo and do

GIT_REPO_NAME=$(basename $(pwd))

You don't have a git repo yet

git init

Create repo on GitHub via CLI

hub create

This command create the repo and add the remote automatically.

Create repo on GitLab via CLI

gitlab create_project $GIT_REPO_NAME "{visibility_level: 20}"

(Public visibility). Source

We will add the remote later, it's part of the trick ;)

Create repo on BitBucket via CLI

bb create --protocol=ssh --scm=git --public $GIT_REPO_NAME


Configuring remotes

Depending on what you want or need, you will have multiple choice to configure your repo.

For a single main repo and simple “mirrors”, you can use this

git remote set-url origin --add${USER}/${GIT_REPO_NAME}.git
git remote set-url origin --add${USER}/${GIT_REPO_NAME}.git

You can check with

git remote -v
origin (fetch)
origin (push)
origin (push)
origin (push)

Now you can just use git push 🙂.

⚠️ Note: to enforce ssh instead of https here is a simple trick

git config --global url.ssh://
git config --global url.ssh://
git config --global url.ssh://

Problem is: git pull will only pull from the first url.

There is inconsitencies with git push --all (push all branches to default remote) and git pull --all (pull from the first url of the default remote).

Here is a detailed post on configuring multiples remote for push, pull or both.

tl;dr: we will have to add other remotes to be able to push.

git remote add origin-gitlab${USER}/${GIT_REPO_NAME}.git
git remote add origin-bitbucket${USER}/${GIT_REPO_NAME}.git

You can double check with:

git remote -v
origin	ssh:// (fetch)
origin	ssh:// (push)
origin	ssh:// (push)
origin	ssh:// (push)
origin-gitlab	ssh:// (fetch)
origin-gitlab	ssh:// (push)
origin-bitbucket	ssh:// (fetch)
origin-bitbucket	ssh:// (push)

Now you can use git push to push to all remotes and use git pull —all to pull from all remotes. My 2 cents: use an alias to pull all by default. If you have a single remote this won’t change anything and will work if you have more than one.

⚠️ You might need a warning that will ask you to do

git branch --set-upstream-to=origin/master master

I use gg to pull and gp to push :)

Pulling from multiple remotes with different updates

Un cas de figure peut être problématique: un commit dans la master sur un repo, un autre commit dans une autre master d’un autre repo, on fetch le tout en local, on veut pousser… Et bam. On va être obligé de force pusher. A part ce cas de figure (qui je l’accorde n’est pas terrible à gérer, mais devrait rester rarissime), je ne vois pas d’autre soucis potentiel. Avec un peu de discipline et de rigueur, on devrait s’en tirer :)

Note about force push


GitLab protect the master branch by default. So force push will not work. I always make one force push or two for the first commit of a project, when CI fail etc (I know I should not). Now you have been warned.

For existing GitHub repo

Je n’ai encore rien tester mais voici 2 outils qui devrait pouvoir aider.

A la limite ajouter les repos au fur et à mesure est aussi une solution, celle que j’adopterais.


Handling issues and Pull/Merge request

Good question. For that, I don’t have the silver bullet. I think I will use GitHub as the main repo. But if there is outage, I will have fallbacks! That’s the idea of this approach: not being tied to a single service.

Commit from web UI

Not a problem. I tried. You commit on the web (eg: comment, notes in README etc). You pull, you push. Done. The origin you edited on the web will be up to date already, but other will be updated.

tl;dr (with all CLI tools installed and configured)

  1. If you GitHub/GitLab/BitBucket is not exactly your $USER
  1. For New repo (if your repo already exist on GitHub, go to 2.)
git init
hub create
  1. For existing GitHub repo
GIT_REPO_NAME=$(basename $(pwd))
gitlab create_project $GIT_REPO_NAME "{visibility_level: 20}"
bb create --protocol=ssh --scm=git --public $GIT_REPO_NAME
git remote set-url origin --add${USER}/${GIT_REPO_NAME}.git
git remote set-url origin --add${USER}/${GIT_REPO_NAME}.git
git remote add origin-gitlab${USER}/${GIT_REPO_NAME}.git
git remote add origin-bitbucket${USER}/${GIT_REPO_NAME}.git
  1. Check that everything is ok
git remote -v
origin  ssh:// (fetch)
origin  ssh:// (push)
origin  ssh:// (push)
origin  ssh:// (push)
origin-bitbucket        ssh:// (push)
origin-bitbucket        ssh:// (fetch)
origin-gitlab   ssh:// (fetch)
origin-gitlab   ssh:// (push)

😇 Now you can just git push and git pull!

Bonus: badges

You can add some nices badges to show the redundancy on your project README

[![Repo on GitHub](](
[![Repo on GitLab](](
[![Repo on BitBucket](](
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment