Skip to content

Instantly share code, notes, and snippets.

@barenko
Last active September 26, 2024 19:53
Show Gist options
  • Select an option

  • Save barenko/dd3f2fef324e352c5b74cfd1c524491e to your computer and use it in GitHub Desktop.

Select an option

Save barenko/dd3f2fef324e352c5b74cfd1c524491e to your computer and use it in GitHub Desktop.
Using multiple remotes with multiple ssh-keys with git (configured by project

WINDOWS

I made this configuration for windows, but you can change it to linux/mac effortlessly.

I'm assuming that you have 2 sshkeys already installed and added in ssh-agent: one personal and other for work (corp).

Change/Create a ~/.ssh/config file with the content:

%USER%/.ssh/config

Host gitlab.com-corp
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/corp_id_ed25519

Host github.com-corp
  HostName github.com
  User git
  IdentityFile ~/.ssh/corp_id_ed25519

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519

Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_ed25519

Inside your project, change the .git/config file with the follow remote configuration:

.git/config

...
[remote "github"]
  url = git@github.com-corp:my-corporation/my-corp-project.git
  fetch = +refs/heads/*:refs/remotes/origin/*
[remote "gitlab"]
  url = git@gitlab.com-corp:my-corporation/my-corp-project.git
  fetch = +refs/heads/*:refs/remotes/gitlab/*
...

Test it:

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