爲什麼本地要使用多個ssh,因爲我管理了多個github賬戶。在github後臺添加ssh,一個ssh不能添加到多個賬戶。
生成多個ssh
ssh-keygen -t ed25519 -f '文件名' -C '郵箱'
生成之後,把公鑰添加到github後臺。
配置~/.ssh/config
#這個是我默認賬戶
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
#test1 隨意寫,爲了方便,可以寫爲賬戶名
Host test1
HostName github.com
User git
IdentityFile ~/.ssh/test1私鑰文件
#test2 隨意寫,爲了方便,可以寫爲賬戶名
Host test2
HostName github.com
User git
IdentityFile ~/.ssh/test2私鑰文件
提交顯示的用戶名、郵箱可以隨便配置
提交代碼之後,會顯示是誰提交的,郵箱是多少。這個可以隨便配置,不影響提交代碼。
git config --global user.email "123"
git config --global user.name "123"
配置倉庫的 ~/.git/config
就是更改url
:把github.com
改爲~/.ssh/config
配置中賬戶對應的Host
。 例如:
url = [email protected]:你的倉庫地址
改爲
url = git@test1:你的倉庫地址
額外說明:如果你是首次提交
要執行下面的命令:
git init
git branch -M main
git remote add origin 你的倉庫地址
git push -u origin main
git branch -M main 很重要,否則會報錯,因爲種族問題,github不允許用master,用main。