天天看點

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,配置檔案中可以忽略此項。

繼續閱讀