How to configure two git account

如何在同一台电脑设置两个git账号。

remove global config

1
2
> git config --list
> vi ~/.git_config

comment user.name and user.email

generate first ssh key

1
> ssh-keygen -t rsa -C "user1@email.com"

get ida_rsa and id_rsa.pub in directory ~/.ssh/

copy id_rsa.pub to github

test

1
> ssh -T git@github.com

generate second ssh key

1
> ssh-keygen -t rsa -f ~/.ssh/id_rsa2 -C "user2@email.com"

get ida_rsa2 and id_rsa2.pub in directory ~/.ssh/

copy id_rsa2.pub to github

test

1
> ssh -T git@git.coding.net -i ~/.ssh/id_rsa2

or add key

1
2
> ssh-agent bash
> ssh-add ~/.ssh/id_rsa2

add config

1
> vi ~/.ssh/config
1
2
3
4
5
6
7
8
9
Host gitlab
HostName 192.168.110.5
User git
IdentityFile ~/.ssh/id_rsa

Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa2

clone

1
2
> git clone github:xxx/xxx.git  # use id_rsa2
> git clone gitlab:xxx/xxx.git # use id_rsa