KO EN

Using Multiple Github Accounts with SSH Key

by Lovefield

ETC /

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 :

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.

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.

 2023-07-12 12.45.08.png

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:

cat first-ssh.pub
 2023-07-12 12.51.28.png

Each account does the above process.

3. Configuration

Edit config file with next command:

vi ~/.ssh/config

Add next content:

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:

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:

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.

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

Now enjoy Git. Thanks.

Author

Lovefield

Lovefield

Web Front-End developer

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

로그인

디코에 오신 것을 환영해요!
전문가들의 수많은 아티클 창고 🤓