首先要装好git,并且配置好第一个git账号
原理
当同步文件夹内 .git
内存在config文件时,git会优先读取配置内容,依次读取私钥内容,而同步的账户则会变成别名账户。
若仓库地址为
[email protected]:dragocytus/project
更改后则按照配置文件来
git@github2:dragocytus/project
下载项目最好通过ssh模式
git clone [email protected]:dragocytus/project.git
若之前通过https下载的,在项目根目录找到 .git 文件夹中的config文件
[remote “origin”]
url = https://将上面内容改成如下内容即可
[remote “origin”]
url = [email protected]:xxx
登录第一个账号
账户设置
git config --global user.name "账户名称"
git config --global user.email "账户邮箱"
生成账户的密钥
# 不添加 `rsa` 参数,默认是用 `ed25519` 算法
ssh-keygen -t rsa -C "账户邮箱"
默认一路回车,密钥会存放在 C:\Users\Administrator/.ssh/
里面,其中有两个文件 id_rsa (私钥),id.rsa.pub(公钥)
将后缀pub
内容,复制到github账户里(见下图),保存。
此时本地账户就会默认此账户为全局账户,通过此账户同步
登录第二个账号设置
1.生成私钥
# 在密钥文件夹中直接生成ssh key文件
cd ~/.ssh
ssh-keygen -t rsa -C "第二个账户的邮箱"
当让输入密钥文件名时,随便起一个,不要跟第一个账号生成的密钥重名(这里是id_rsa_2)
Enter file in which to save the key /c/Users/Administrator/.ssh/id_rsa): id_rsa_2
2.将生成的密钥添加到ssh agent中
ssh-add ~/.ssh/id_rsa_2
```bash
# 如果出现 Could not open a connection to your authentication agent的错误,这是由于bash未挂载到ssh下面,输入下面命令,然后再输入第一条命令即可
ssh-agent bash
3.将id_rsa_2.pub中内容复制到邮箱对应的github账户中去
4.修改配置文件
在~/.ssh 文件夹中找到config文件,若不存在则新建config文件
配置内容如下
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Host github2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_2
评论区