天天看点

win10 Git 同一域名下登录多个不同账号 以及 SSH 和 Http 配置

gitee.com

为例,github、gitlab 等同理

问题场景

你在gitee上有多个账号,或者说多个人在同一个电脑上使用自己的gitee账号,

拉取、推送同域名下不同账号的仓库,需要用不同的账号;

适用于 https 方式账号密码登录

  1. 打开普通凭据:

    控制面板 —— 用户帐户 —— 凭据管理器 —— 普通凭据

  2. 点击【添加普通凭据】
  3. 键入网站地址和凭据信息

    Internet 地址或网络地址 :

    git:https//用户名或邮箱@gitee.com

    用户名:

    用户名或邮箱

    密码:

    你的登录gitee的密码

  4. 保存,点击【确定】

说明:

用户名或邮箱,就是你登录gitee的用户名或邮箱

适用于 SSH 方式登录

参考:

https://gitee.com/help/articles/4181#article-header0

https://gitee.com/help/articles/4229#article-header0

多个不同域名配置

  1. 生成一个SSH-Key

    ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitee_ABC_id_rsa

    [email protected] 只是生成的 sshkey 的名称, 可以使用任意字符串,只要能区分出不同的sshkey即可
  2. 按照提示完成三次回车,即可生成 ssh key
  3. 在 ~/.ssh 目录下新建一个config文件,添加如下内容
# 假如这里是 gitee 的配置项
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_ABC_id_rsa
IdentitiesOnly yes

# 假如这里是 github 的配置项
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
IdentitiesOnly yes
           
  1. 添加生成的 public key 添加到仓库中

    A. 打开生成的公钥:

    cat ~/.ssh/gitee_ABC_id_rsa.pub

    或者 用记事本打开这个文件

    B. 复制其中的内容

    C. 通过

    仓库主页 「管理」->「部署公钥管理」->「添加部署公钥」

    ,添加生成的 public key 添加到仓库中。
  2. 测试

    ssh -T [email protected]

    首次使用需要确认并添加主机到本机SSH可信列表。

    若返回

    Hi XXX! You've successfully authenticated, but Gitee.com does not provide shell access.

    内容,则证明添加成功。

基于多账号配置, 设置同域名下不同账号

  1. 查看配置 remote.origin.url:

    git config -l

  2. 得到配置:

    [email protected]:XXXX/test.git

  3. 设置自定义提交url 的 host:

    git remote set-url origin [email protected]_gitee_ABC:XXXX/test.git

  4. 修改 ~/.ssh/config 配置文件,如下
# gitee
Host host_gitee_ABC 
HostName gitee.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_ABC_id_rsa
IdentitiesOnly yes
           

说明:

host_gitee_ABC 是自定义的一个字符串, config配置中

Host host_gitee_ABC

git remote set-url origin [email protected]_gitee_ABC:XXXX/test.git

保持一致即可

config配置含义

Host

用来指定Host名字,此处必须使用本地

[email protected]:XXXX/test.git

gitee.com

,二者必须保持一致

git在config中找到对应的Host,就会使用该配置

HostName

此处指定Host对应的具体域名,git 提交到远端具体使用的域名

User git

说明该配置的用户是 git, 就是

[email protected]:XXXX

中@前的git

IdentityFile ~/.ssh/gitee_ABC_id_rsa

指定了具体使用哪个私钥文件

IdentitiesOnly yes

配置为yes,配置文件中可以忽略此项。

继续阅读