How to config multi git accounts

The background is i have to access both github and company git account on my working computer, i wish it can switch git account automatically.

Generate ssh pub & private key

1
$ ssh-keygen -t rsa -C "your_github_email@example.com"

Add ssh pub key to github settings

Create independence ssh configuration file for different git account

1
2
3
4
root directory:~\\.ssh 
- /company/.gitconfig-company
- /github/.gitconfig-github
- .config

Set configuration file

1
2
3
4
5
.gitconfig-company

[User]
name = username
email = company git account email
1
2
3
4
5
.gitconfig-github

[User]
name = username
email = github account email
1
2
3
4
5
6
7
8
9
10
11
12
.config

#git for company
Host
User
IdentityFile ~/.ssh/company/zhaowh3_rsa

#git for github
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github/id_rsa_github