GitHub_multiple_account
Setup Multiply Github account via SSH
1. Generate separate SSH keys
1 | # personal account |
it will generate file private and public:
1 | ~/.ssh/id_personal |
2. Add public keys to GitHub
- GitHub → Settings
- SSH and GPG keys
- New SSH key
- Paste the
.pubfile content
3. Registering the new SSH Keys with the ssh-agent
1 | eval "$(ssh-agent -s)" |
- List key:
ssh-add -l - 刪除則使用:
ssh add -d ~/.ssh/id_personal - 刪除全部:
ssh-add -D
4. confgiure ssh by create SSH config File
create ssh config
Edit config file1
nano ~/.ssh/config
or manually create it
1
2
3cd ~/.ssh/
touch config // Creates the file if not exists
code config // Opens the file in VS code, use any editorchange permission:
chmod 600 ~/.ssh/configEdit config
1
2
3
4
5
6
7
8
9
10
11
12
13
# Personal account, - the default config
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_personal
IdentitiesOnly yes
# Work account-1
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work_user1
IdentitiesOnly yes
Note:
IdentitiesOnly yes: Use only this key, no guessing. prevents SSH from trying the wrong keys
5. test ssh work
1 | ssh -T git@github-work |
6. clone use correct ssh host
When git clone you need to change it’s hostname from git@github to setting you se in .ssh/config this distinguish multiply account.
If i was to git clone my repo like below picture which github gave URL git@github.com:jakccl/helloTest.git need to change to git@github-personal. If you only have one account then you can use what github gave git@github.com
1 | #work |
7. Git identity per repo(optional)
global
1
2
3[user]
name = Jak Lee
email = personal@gmail.comPersonal
1
2
3cd project
git config user.name "Jak Lee"
git config user.email "personal@gmail.com"work
1
2
3cd project2
git config user.name "Jak Lee"
git config user.email "work@company.com"Note:
These settings are stored inproject1/.git/configandproject2/.git/config
They override global~/.gitconfigfor that repocheck:
git config --get user.email
A better way might be seperate folder for work and personal.
Create Seperate Directory for github
1 | ~/Code/ |
1. configure git global config
1 |
|
2. Personal ~Code/personal/.gitconfig
~Code/personal/.gitconfig
1 | [user] |
3. work ~Code/work/.gitconfig
1 | [user] |
4. Check and verify
check config
1
2git config --show-origin --get user.email
git config --show-origin --get user.nameother git command
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 修正 remote URL 為 SSH
git remote set-url origin git@<你剛才設定的 SSH Host 名稱>:<Github 用戶名稱>/<Repo 名稱>.git
# 確認配置自動套用
git config user.name
git config user.email
# 檢查目前專案設定
git config --local --list
# 確認 remote URL
git remote -v
# 測試 SSH 連線
ssh -T github.com-personal
ssh -T github.com-work