TECH BLOG

Logo

Make It Easy Togather

View the Project on GitHub TansanSui-H2CO3/tech_blog_h2co3

Manage Multiple Git Accounts Automatically

2021-02-05 08:22:00 +0000

Introduction

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.

Generate Public Keys

  1. Move to folder where a key is saved.
    $ cd ~/.ssh
    
  2. Generate a public key named as 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:
    
  3. Generate a public key named as 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:
    

Register Each Public Key to Each Account (Public/Private)

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).

Setting Configuration of SSH

Cloning Repositories

You cannot use git clone git@github.com:[userName]/[repositoryName]. You should use the following commands.

Change .gitconfig

To manage multiple account, you should not set global user, but local user.

  1. Move to repository

  2. Delete global user information
    git config --global --unset user.name
    git config --global --unser user.email
    
  3. Set local user’s name, email, insteadOf
    • If the repository is for public account…
       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"
      
    • If the repository is for private account…
       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.

Conclusion

By the above process, I can manage two git account automatically :)

Reference