You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
title: How to keep in sync your Git repos on GitHub, GitLab & Bitbucket easily
tags:
- git
- github
- gitlab
- bitbucket
authors:
- MoOx
---
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).
[tl;dr?](#tl-dr)
## Git Tooling
In order to facilitate the setup, we will install some CLI tools for each services.
### Github
We will use [hub](https://github.com/github/hub).
For macOS, it’s easy.
```console
brew install hub
```
See [Hub installation instruction page](https://github.com/github/hub#installation) for others OSes.
You will need a [GitHub token](https://github.com/settings/tokens/new).
Place it in your home folder in a ``.github_token`` file,
and load it in your `.bash/zshrc` like this:
```sh
if [[ -f$HOME/.github_token ]]
then
export GITHUB_TOKEN=$(cat $HOME/.github_token)
fi
```
### GitLab
[GitLab CLI](http://narkoz.github.io/gitlab/cli) is available via [rubygem](https://rubygems.org/):
```console
(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](https://gitlab.com/profile/account)
and use the same solution as GitHub.
Here is an example using GitLab “official” online instance that you should add in your ``.bash/zshrc``:
[BitBucket CLI](https://bitbucket.org/zhemao/bitbucket-cli) is available via [pip](https://pip.pypa.io/en/stable/):
```sh
(sudo) pip install bitbucket-cli
```
BitBucket does not work well with a token…
2fa is not convenient (and [impossible to use with ssh](https://bitbucket.org/zhemao/bitbucket-cli/issues/25/create-issue-ssh-not-taken-in))
So you will have to enter login/pwd all the time or [put that in clear in a .bitbucket file](https://bitbucket.org/zhemao/bitbucket-cli#markdown-header-configuration)
---
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
```console
GIT_REPO_NAME=$(basename $(pwd))
```
### You don't have a git repo yet
```console
GIT_REPO_NAME="your-repo"
mkdir $GIT_REPO_NAME && cd $GIT_REPO_NAME
git init
```
### Create repo on GitHub via CLI
```console
hub create
```
This command create the repo and add the remote automatically.
**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](https://astrofloyd.wordpress.com/2015/05/05/git-pushing-to-and-pulling-from-multiple-remote-locations-remote-url-and-pushurl/).
_tl;dr: we will have to add other remotes to be able to 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
open https://gitlab.com/${USER}/${GIT_REPO_NAME}/protected_branches
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.
https://pypi.python.org/pypi/github2gitlab
https://github.com/xuhdev/backup-on-the-go
A la limite ajouter les repos au fur et à mesure est aussi une solution, celle que j’adopterais.
## FAQ
### 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)
0. If you GitHub/GitLab/BitBucket is not exactly your $USER
```
USER=MoOx
```
1. For New repo (if your repo already exist on GitHub, go to 2.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to keep in sync your Git repos on GitHub, GitLab & Bitbucket easily
---
title: How to keep in sync your Git repos on GitHub, GitLab & Bitbucket easily
tags:
- git
- github
- gitlab
- bitbucket
authors:
- MoOx
---
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).
[tl;dr?](#tl-dr)
## Git Tooling
In order to facilitate the setup, we will install some CLI tools for each services.
### Github
We will use [hub](https://github.com/github/hub).
For macOS, it’s easy.
```console
brew install hub
```
See [Hub installation instruction page](https://github.com/github/hub#installation) for others OSes.
You will need a [GitHub token](https://github.com/settings/tokens/new).
Place it in your home folder in a ``.github_token`` file,
and load it in your `.bash/zshrc` like this:
```sh
if [[ -f$HOME/.github_token ]]
then
export GITHUB_TOKEN=$(cat $HOME/.github_token)
fi
```
### GitLab
[GitLab CLI](http://narkoz.github.io/gitlab/cli) is available via [rubygem](https://rubygems.org/):
```console
(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](https://gitlab.com/profile/account)
and use the same solution as GitHub.
Here is an example using GitLab “official” online instance that you should add in your ``.bash/zshrc``:
[BitBucket CLI](https://bitbucket.org/zhemao/bitbucket-cli) is available via [pip](https://pip.pypa.io/en/stable/):
```sh
(sudo) pip install bitbucket-cli
```
BitBucket does not work well with a token…
2fa is not convenient (and [impossible to use with ssh](https://bitbucket.org/zhemao/bitbucket-cli/issues/25/create-issue-ssh-not-taken-in))
So you will have to enter login/pwd all the time or [put that in clear in a .bitbucket file](https://bitbucket.org/zhemao/bitbucket-cli#markdown-header-configuration)
---
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
```console
GIT_REPO_NAME=$(basename $(pwd))
```
### You don't have a git repo yet
```console
GIT_REPO_NAME="your-repo"
mkdir $GIT_REPO_NAME && cd $GIT_REPO_NAME
git init
```
### Create repo on GitHub via CLI
```console
hub create
```
This command create the repo and add the remote automatically.
**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](https://astrofloyd.wordpress.com/2015/05/05/git-pushing-to-and-pulling-from-multiple-remote-locations-remote-url-and-pushurl/).
_tl;dr: we will have to add other remotes to be able to 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
open https://gitlab.com/${USER}/${GIT_REPO_NAME}/protected_branches
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.
https://pypi.python.org/pypi/github2gitlab
https://github.com/xuhdev/backup-on-the-go
A la limite ajouter les repos au fur et à mesure est aussi une solution, celle que j’adopterais.
## FAQ
### 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)
0. If you GitHub/GitLab/BitBucket is not exactly your $USER
```
USER=MoOx
```
1. For New repo (if your repo already exist on GitHub, go to 2.)