本地使用多个github账户-ssh

为什么本地要使用多个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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章