Git ssh 配置多账户

本文介绍了在Windows上使用Gitbash为不同的GitHub用户创建和管理私钥公钥对。通过ssh-keygen生成密钥,并在~/.ssh目录下创建config文件来指定不同用户的SSH身份。在拉取仓库时,如gitclone命令,系统会根据config设置使用相应的密钥进行身份验证。
摘要由CSDN通过智能技术生成

注意:如果是 windows 需要使用 Git bash

创建私钥公钥


ssh-keygen -t <文件名称> -C “<注释内容>”

ssh-keygen -t user_1 -C “user_1” # 用户1
ssh-keygen -t user_2 -C “user_2” # 用户2

执行命令后,会在 ~/.ssh 目录下生成 user_1user_1.pub 两个文件,其中 user_1.pub 就是你的公钥
使用命令 cat ~/.ssh/user_1.pub 查看公钥
复制到 Git 仓库 ssh 配置项中

新建并配置 config 文件


在 ~/.ssh 目录下新建 config 文件,无需后缀,
往里面添加内容

# 示例用户1
Host user_1
HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/user_1
  
# 示例用户2
Host user_2
HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/user_2

链接


这里以请求 https://github.com/netchx/netch.git 为例

git clone https://user_1/netchx/netch.git    # 将使用 user 1 的密钥
git clone https://user_2/netchx/netch.git    # 将使用 user 2 的密钥

重要!!!


在拉仓库的时候
git clone https://user_1/netchx/netch.git

会有提示,
Are you sure you want to continue connecting (yes/no/[fingerprint])?
这里一定要输入 yes
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值