Using Multiple Github Accounts with SSH Key

GIT

(Update : 2024-11-28)

Language :

Hi, I’m Lovefield.

Recently, I had to use two Github accounts on one computer. I don’t like to use https protocol and I want to use SSH Key. So I researched it many ways. This article is about using two Github accounts in one computer with SSH Key.

1. Setting SSH Key

In MAC, Change directory with use cd ~/.ssh command. And use next command :

shell

ssh-keygen -t ed25519 -C “first@email.com” -f first-ssh
ssh-keygen -t ed25519 -C “second@email.com” -f second-ssh

If it is successful, this command will make 4 files.

  • first-ssh
  • first-ssh.pub
  • second-ssh
  • second-ssh.pub

Now use the `ssh-add` command and add SSH Key.

shell

ssh-add first-ssh && ssh-add second-ssh

You can use the ssh-add -L command to see if it is successful.

2. Add SSH Key in Github account

Go to the Github site and go to `setting > Access > SSH and GPG keys` page.

Click to upper right “New SSH Key” button. And then add SSH Key.Add content to the “Key” field. You can get content use next command:

shell

cat first-ssh.pub

Each account does the above process.

3. Configuration

Edit config file with next command:

shell

vi ~/.ssh/config

Add next content:

text

Host first.github.com
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  User git
  IdentityFile ~/.ssh/first-ssh
  IdentitiesOnly yes
Host second.github.com
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  User git
  IdentityFile ~/.ssh/second-ssh
  IdentitiesOnly yes

If the correction is done. Using next command to check print console to each accounts:

shell

ssh -T git@first.github.com
ssh -T git@second.github.com

Is successful if that command printed the right account name. Now, when you use clones in Github, you must use each of the following next:

shell

git clone git@first.github.com:userName/RepoName
git clone git@second.github.com:userName/RepoName

When you use the git command, you must set the config file for each local repo.

shell

git config user.email “second@email.com”
git config user.nickname “second”

Now enjoy Git. Thanks.

Lovefield

Web Front-End developer

하고싶은게 많고, 나만의 서비스를 만들고 싶은 변태스러운 개발자입니다.