Skip to content

Instantly share code, notes, and snippets.

@RezhaBlue
Last active January 24, 2020 15:20
Show Gist options
  • Save RezhaBlue/aad9976b304f615be3e4472579fc068d to your computer and use it in GitHub Desktop.
Save RezhaBlue/aad9976b304f615be3e4472579fc068d to your computer and use it in GitHub Desktop.

Setup SSH for and push to multiple Git-hosting service accounts (including self-hosted) from scratch

Thanks to Rodney Lorrimar for Pushing to Multiple Git Repos

This example uses two GitHub accounts, and a self-hosted GitLab account (localhost:8092 for ssh).

The Github (another-user account) private repo has been arbitrarily chosen as the main repo for the project.

The mirrors are a separate Github (wellknownuser account) public project, and a Gitlab (dubioususer account) private group and project.

The workspace is Ubuntu via WSL, the C:/ drive is mounted to /c/.

The workspace is /c/workspace/.

Stage 1. Setup SSH Keys

Remove any ssh identities present

eval $(ssh-agent -s)

Agent pid 464

ssh-add -D

All identities removed.
Do not do this if you want to keep your saved identities.

ssh-add -l

The agent has no identities.

Generate SSH keys for each Git-hosting service account

ssh-keygen -t rsa -b 4096 -C "[email protected]"

produces ~/.ssh/local_gitlab_du

ssh-keygen -t rsa -b 4096 -C "[email protected]"

produces ~/.ssh/github_au

ssh-keygen -t rsa -b 4096 -C "[email protected]"

produces ~/.ssh/github_wku

Add SSH keys to Git-hosting service account

Gitlab

localhost:8092 > Settings > SSH keys:

Key: value of local_gitlab_du.pub
Title: local_gitlab_du

Add Key

Github

another-user's github > Settings > SSH and GPG keys > New SSH key:

Title: github_au
Key: value of github_au.pub

Add SSH key

wellknownuser's github > Settings > SSH and GPG keys > New SSH key:

Title: github_wku
Key: value of github_wku.pub

Add SSH key

Create SSH Config file

vi ~/.ssh/config

# Local gitlab
Host gitlab
  Hostname localhost
  Port 8092
  User Administrator
  IdentityFile ~/.ssh/local_gitlab_du

# Github another-user
Host github-au
  Hostname github.com
  User another-user
  IdentityFile ~/.ssh/github_au

# Github wellknownuser
Host randomalias-wku
  Hostname github.com
  User wellknownuser
  IdentityFile ~/.ssh/github_wku

:wq!

Stage 2. Setup the project remotes

In each account, create the project repo if non-existent

Get the clone urls of the project repos

Replace account hostname (e.g. github.com or localhost:8092) in each url with ~/.ssh/config's host e.g.

git@github-au:another-user/some_proj.git
git@randomalias-wku:wku20xx/some_proj.git
ssh://git@gitlab/someprivategroup/some_proj.git

Clone the project down from the main account (another-user account)

git clone git@github-au:another-user/some_proj.git

Cloning into 'some_proj'...

git remote -v

origin git@github-au:another-user/some_proj.git (fetch)
origin git@github-au:another-user/some_proj.git (push)

git remote show origin

* remote origin
Fetch URL: git@github-au:another-user/some_proj.git
Push URL: git@github-au:another-user/some_proj.git
HEAD branch: master
Remote branch:
  master tracked
Local branch configured for 'git pull':
  master merges with remote master
Local ref configured for 'git push':
  master pushes to master (up to date)

cd some_proj

Set the mirror clone URLs as remote push URLs.

Note the ssh protocol in the GitLab clone url

git remote set-url --add --push origin ssh://git@gitlab/someprivategroup/some_proj.git

This removes the main account from its place as a remote push url.

git remote show origin

* remote origin
Fetch URL: git@alias-au:another-user/some_proj.git
Push URL: ssh://git@gitlab/someprivategroup/some_proj.git
HEAD branch: master
Remote branch:
  master tracked
Local branch configured for 'git pull':
  master merges with remote master
Local ref configured for 'git push':
  master pushes to master (up to date)

git remote set-url --add --push origin git@randomalias-wku:wku20xx/some_proj.git

git remote show origin

* remote origin
Fetch URL: git@alias-au:another-user/some_proj.git
Push URL: ssh://git@gitlab/someprivategroup/some_proj.git
Push URL: git@randomalias-wku:wku20xx/some_proj.git
HEAD branch: master
Remote branch:
  master tracked
Local branch configured for 'git pull':
  master merges with remote master
Local ref configured for 'git push':
  master pushes to master (up to date)

Retrieve the main account url, and set it back to a remote push url.

git remote set-url --add --push origin git@alias-au:another-user/some_proj.git

git remote show origin

* remote origin
Fetch URL: git@alias-au:another-user/some_proj.git
Push URL: ssh://git@gitlab/someprivategroup/some_proj.git
Push URL: git@randomalias-wku:wku20xx/some_proj.git
Push URL: git@alias-au:another-user/some_proj.git
HEAD branch: master
Remote branch:
  master tracked
Local branch configured for 'git pull':
  master merges with remote master
Local ref configured for 'git push':
  master pushes to master (up to date)

Stage 3. Push content to the project repos in each account.

This assumes you have placed content into the cloned down project

git push -u origin --all

Enter passphrase for key '/home/winbuntu/.ssh/local_gitlab_du':
Branch 'master' set up to track remote branch 'master' from 'origin'.
Everything up-to-date
Counting objects: 15, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (15/15), 5.64 KiB | 5.64 MiB/s, done.
Total 15 (delta 6), reused 15 (delta 6)
To ssh://gitlab/someprivategroup/some_proj.git
 * [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Warning: Permanently added the RSA host key for IP address 'XXX.XXX.XXX.XXX' to the list of known hosts.
Branch 'master' set up to track remote branch 'master' from 'origin'.
Everything up-to-date
Counting objects: 15, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (15/15), 5.64 KiB | 5.64 MiB/s, done.
Total 15 (delta 6), reused 15 (delta 6)
remote: Resolving deltas: 100% (6/6), done.
To randomalias-wku:wku20xx/some_proj.git
 * [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment