1

I have two different git accounts.

Account 1 GitHub githubaccount@email.com

Account 2 GitLab gitlubaccount@email.com

In my .ssh folder I have a SSH Key from my GitHub account. In the config file I have

Host *
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/id_edxxxxx

In my git config --global user.mail and name I have the GitLab account information.

My question is the following, how can I use both accounts with their respective SSH keys for different projects? Is it possible?

cblnpa
  • 397
  • 1
  • 4
  • 20
  • 2
    You can certainly use different ssh keys for different sites. Just add more `Host` lines along with `IdentitiesOnly yes` under those host lines, so that you tell ssh to present *only* the *listed* identities to that host. (That is, you'll have multiple `IdentityFile` lines, one under each `Host`, with the GitHub one giving the GitHub key, and the GitLab one giving the GitLab key, and so on.) See [this answer in particular](https://stackoverflow.com/a/26507643/1256452) for examples. The question is different, but the answer is entirely applicable. – torek May 16 '22 at 21:35
  • Thank you so much @torek I needed that explanation! – cblnpa May 20 '22 at 14:53
  • Does this answer your question? [Multiple GitHub Accounts & SSH Config](https://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config) – tripleee Aug 15 '22 at 06:40

2 Answers2

1

I suggest generating a single SSH key and adding the public key it to both your GitLab and GitHub accounts. The emails you set for each local repo don't matter. These are only used to set on commits and have nothing to do with authentication with remote services.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    Re-using ssh keys in multiple places is certainly the easiest way. Some frown on it for security reasons though (one compromised key results in multiple compromised destinations). – torek May 16 '22 at 21:33
  • 1
    Thanks @Code-Apprentice now I'm using a single SSH key with my two accounts, but as torek says, probably isn't the best security practice. But for now is working and I'm the only one using this laptop – cblnpa May 20 '22 at 14:55
  • @cblnpa Yes, there is certainly some risk. As long as you secure the private key, you should be fine. – Code-Apprentice May 20 '22 at 15:19
  • @Code-Apprentice exactly! – cblnpa May 20 '22 at 15:19
1

In my git config --global user.mail and name I have the GitLab account information.

This has nothing to do with authentication: you could have xxx and xxx@yyy.com, that would be the same.

how can I use both accounts with their respective SSH keys for different projects?

That means two key:

  • the default one ~/.ssh/id_edxxxxx: its public key should be registered to user1
  • ssh-keygen -t ed25519 -P "" -f ~/.ssh/gh2: the gh2.pub should be regered to user2.

From there, add a ~/.ssh/config file with

Host gh1
User git
Port 22
Hostname github.com
IdentityFile ~/.ssh/id_ed25519
TCPKeepAlive yes
IdentitiesOnly yes

Host gh2
User git
Port 22
Hostname github.com
IdentityFile ~/.ssh/gh2
TCPKeepAlive yes
IdentitiesOnly yes

Replace your remote URL by:

cd /path/to/project1/local/clone
git remote set-url origin gh1:User1/project1

cd /path/to/project2/local/clone
git remote set-url origin gh2:User2/project2

That way, "both accounts with their respective SSH keys for different projects".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you so much @VonC I had no idea that the git config --global ... has nothing to do with authentication, that clarified a lot of things to me, thanks! – cblnpa May 20 '22 at 14:56