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/
.
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.
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
localhost:8092 > Settings > SSH keys:
Key: value of local_gitlab_du.pub
Title: local_gitlab_du
Add Key
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
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!
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
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
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)
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)
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'.