Contents
Outline
Sometimes you may want to use both your company’s GitHub account and your personal GitHub account on one machine (PC). In this blog post, I will introduce how to set up multiple GitHub accounts on one machine.
Create SSH key
To use multiple GitHub accounts, you need to create an SSH key. I use macOS
, so I will explain based on macOS
.
First, go to the .ssh
directory.
cd ~/.ssh
Next, use the ssh-keygen
command to create an SSH key. First, create an SSH key for your personal account.
ssh-keygen -t rsa -b 4096 -C "GITHUB_PERSONAL_ACCOUNT_EMAIL"
Then, you will be asked what name to save the key as.
Enter file in which to save the key (/Users/user_name/.ssh/id_rsa):
In my case, I saved it as id_rsa_personal
for easy distinction. Next, create an SSH key for your company account.
ssh-keygen -t rsa -b 4096 -C "GITHUB_COMPANY_ACCOUNT_EMAIL"
Again, you will be asked what name to save the key as.
Enter file in which to save the key (/Users/user_name/.ssh/id_rsa):
In my case, I saved it as id_rsa_work
for easy distinction.
Register SSH key on GitHub
You need to register the generated SSH key on GitHub. First, let’s register the SSH key for the personal account.
Run the following command to check the SSH key.
cat ~/.ssh/id_rsa_personal.pub
Copy the displayed SSH key and go to the GitHub website. Click Settings
-> SSH and GPG keys
-> New SSH key
to register the SSH key. You need to be logged in with your personal account on the GitHub website.
Register the SSH key on the GitHub website in the same way for the company account. You need to be logged in with your company account on the GitHub website.
Modify config file
To distinguish between GitHub accounts using the SSH key you set up, modify the ~/.ssh/config
file as follows.
# personal
Host personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
# work
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
Then, execute the following command to check if the SSH key is registered.
ssh -T git@personal
If the following message appears, the SSH key has been successfully registered.
Hi GITHUB_PERSONAL_ACCOUNT! You've successfully authenticated, but GitHub does not provide shell access.
In the same way, execute the following command to check if the SSH key is registered for the company account.
ssh -T [email protected]
If the following message appears, the SSH key has been successfully registered.
Hi GITHUB_COMPANY_ACCOUNT! You've successfully authenticated, but GitHub does not provide shell access.
Clone
Now, when you clone a project from GitHub, you can distinguish between your personal account and your company account as follows.
# Personal account
git clone git@personal:GITHUB_PERSONAL_ACCOUNT/REPOSITORY.git
# Company account
git clone [email protected]:GITHUB_COMPANY_ACCOUNT/REPOSITORY.git
Git user settings
When you execute the following command, you can check the user.name
and user.email
set in Git
.
# Check user name
git config user.name
# Check user email
git config user.email
If you have not registered user information in the Global
settings of Git
, nothing will appear when you execute the command.
You can set the Global
user and set the user to use in specific folders by modifying the .gitconfig
file.
First, create the ~/.gitconfig-work
file and the ~/.gitconfig-personal
file and modify them as follows.
~/.gitconfig-work
# code ~/.gitconfig-work
[user]
name = "GITHUB_COMPANY_ACCOUNT_USER_NAME"
email = GITHUB_COMPANY_ACCOUNT_EMAIL
~/.gitconfig-personal
# code ~/.gitconfig-personal
[user]
name = "GITHUB_PERSONAL_ACCOUNT_USER_NAME"
email = GITHUB_PERSONAL_ACCOUNT_EMAIL
Then, modify the .gitconfig
file as follows.
# code ~/.gitconfig
# This is for Global settings
[user]
name = "GITHUB_COMPANY_ACCOUNT_USER_NAME"
email = GITHUB_COMPANY_ACCOUNT_EMAIL
# For company account
[includeIf "gitdir:~/WORK/"]
path = ~/.gitconfig-work
# For personal account
[includeIf "gitdir:~/PERSONAL/"]
path = ~/.gitconfig-personal
In this setting, the user information for the company account is set as the Global
user of Git
. In the WORK
folder, the company account is used, and in the PERSONAL
folder, the personal account is used.
To check if the settings are correct, go to the PERSONAL
folder. Then, execute the following command to check the user information.
cd ~/PERSONAL
git config user.name
git config user.email
Then, the Global
user information of Git
will be displayed as follows.
# git config user.name
GITHUB_COMPANY_ACCOUNT_USER_NAME
# git config user.email
GITHUB_COMPANY_ACCOUNT_EMAIL
This is because there is no Git
project in the current folder (PERSONAL
) at the moment. So, the Global
user information of Git
is displayed.
Next, let’s create an actual Git
project and check the user information of Git
.
mkdir ~/PERSONAL/temp
cd ~/PERSONAL/temp
git init
git config user.name
git config user.email
Then, then personal account user information of Git
will be displayed as follows.
GITHUB_PERSONAL_ACCOUNT_USER_NAME
GITHUB_COMPANY_ACCOUNT_EMAIL
This is because we have set the personal account user information in the .gitconfig
file as follows.
[includeIf "gitdir:~/PERSONAL/"]
path = ~/.gitconfig-personal
Completed
Done! We’ve seen how to use multiple GitHub accounts on one machine. Now you can manage projects without any inconvenience using multiple GitHub accounts on one machine.
Also, we’ve seen how to set the user to use in specific folders using the .gitconfig
file. You can now easily change the user information of Git
.
If you need to use multiple GitHub
accounts, I hope you can refer to this blog post and set up and use multiple GitHub
accounts.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
App promotion
Deku
.Deku
created the applications with Flutter.If you have interested, please try to download them for free.