Make It Easy Togather
2021-02-05 08:22:00 +0000
I wanna manage multiple git account such as public (for individual) and private (for work). In this post, I write how to manage two (public and private) git accounts, and you can extend the number of git accounts by applying the following processes.
$ cd ~/.ssh
public_rsa.pub
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/(username)/.ssh/id_rsa):public_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
private_rsa.pub
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/(username)/.ssh/id_rsa):private_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
You can copy the contents of hoge_rsa.pub
to clipboard with the following command.
$ clip < ~/.ssh/hoge_rsa.pub
After that, registering the SSH key to GitHub account (Settings -> SSH and GPG keys -> SSH keys -> New SSH key).
config
file.
vi ~/.ssh/config
# public SSH configuration
Host github_public
HostName github.com
User git
IdentifierFile ~/.ssh/public_rsa
IdentitiesOnly yes
AddKeysToAgent yes
# private SSH configuration
Host github_private
HostName github.com
User git
IdentifierFile ~/.ssh/private_rsa
IdentitiesOnly yes
AddKeysToAgent yes
You cannot use git clone git@github.com:[userName]/[repositoryName]
. You should use the following commands.
$ git clone git@github_public:[userName]/[repositoryName]
$ git clone git@github_private:[userName]/[repositoryName]
.gitconfig
To manage multiple account, you should not set global user, but local user.
Move to repository
git config --global --unset user.name
git config --global --unser user.email
git config --local user.name "Your public name"
git config --local user.email "Your public e-mail address"
git config --local url."github_public".insteadOf "git@github.com"
git config --local user.name "Your private name"
git config --local user.email "Your private e-mail address"
git config --local url."github_private".insteadOf "git@github.com"
Since you should register the above information when cloning repositories, it is recommended to create alias in .bash_profile
.
By the above process, I can manage two git account automatically :)