Switch between multiple local Git accounts on MacOS with ZSH
Varsion

Now, I have two GitHub Account on my macbook.

One of the accounts is my work account and the other is my personal account, I will write some code of my own after get off work or when i free.

About Github SSH refer to this document: Github - Connect with SSH.

Now you have created two github ssh keys.

SHH config

for macos add the config file into .ssh/, now the .ssh/ florder tree like this:

1
2
3
4
5
6
7
8
~/.ssh
✘ tree
.
├── config
├── account_1
├── account_1.pub
├── account_2
└── account_2.pub

and add the settings into config:

1
2
3
4
5
6
7
8
9
10
11
Host account_1
AddKeysToAgent yes
HostName github.com
User account_1
IdentityFile ~/.ssh/account_1

Host account_2
AddKeysToAgent yes
HostName github.com
User account_2
IdentityFile ~/.ssh/account_2

ZSH alias

add the alias into .zshrc

1
2
alias gu-account-1="ssh-add -d ~/.ssh/account_1 && ssh-add ~/.ssh/account_1 && ssh -T git@github.com"
alias gu-account-2="ssh-add -d ~/.ssh/account_1 && ssh-add ~/.ssh/account_2 && ssh -T git@github.com"

gu-account-1 => git user account-1 🤣

Or you can freely name it according to your habits, it is all possible.

Then we can easily switch the current github account:

image-20220518181930645

I hope my experience can help you and make you happier writing code.

  • Post title:Switch between multiple local Git accounts on MacOS with ZSH
  • Post author:Varsion
  • Create time:2022-05-18 11:33:51
  • Post link:https://blog.varsion.cn/post/46e8ee7c.html
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
Comments