天天看點

同一用戶端下使用多個git賬号

同一用戶端下使用多個git賬号

1. 生成新的 SSH keys

在使用者主目錄下(

cd ~

然後

pwd

,即可看到使用者主目錄路徑)的.ssh目錄中打開git bash

ssh-keygen -t rsa -C "[email protected]"

ssh-keygen -t rsa -C "[email protected]"

在生成第一組 id_rsa 和 id_rsa.pub_ 可以選用預設的檔案名,在出現提示輸入檔案名的時候要輸入一個不同的檔案名,比如:這裡填的是

id_rsa_new

Enter file in which to save the key (~/.ssh/id_rsa): id_rsa_new

2. 将公鑰添加到GitHub的 SSH 設定中

3. 配置 ~/.ssh/config 檔案

通過以上步驟,公鑰、密鑰分别被添加到 git 伺服器和本地了。下面我們需要在本地建立一個密鑰配置檔案,通過該檔案,實作根據倉庫的 remote 連結位址自動選擇合适的私鑰。

# 該檔案用于配置私鑰對應的伺服器

Host ggbond6    # 别名,随便定 後面配置位址有用

   HostName ssh.github.com

   User git

   IdentityFile ~/.ssh/id_rsa_ggbond6

Host ggbondd   # 别名,随便定 後面配置位址有用

   IdentityFile ~/.ssh/id_rsa_ggbondd

4. 測試一下

ssh -T git@ggbond6

SSH -T git@ggbondd

# 成功傳回

Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access.

5. 本地倉庫的使用者配置

git 的配置分為三級别,System —> Global —>Local。System 即系統級别,Global 為配置的全局,Local 為倉庫級别,優先級是 Local > Global > System。

檢視配置

git config --local -l

git config --global -l

git config --system -l

清除 Git 的全局設定

git config --global --unset user.name

git config --global --unset user.email

是以我們需要為每個倉庫單獨配置使用者名資訊,假設我們要配置 github 的某個倉庫,進入該倉庫後,執行:

git config --local user.name "ggbondd"

git config --local user.email "[email protected]"

執行完畢後,通過以下指令檢視本倉庫的所有配置資訊:

至此你已經配置好了 Local 級别的配置了,此時送出該倉庫的代碼,送出使用者名就是你設定的 Local 級别的使用者名了

6. 綁定遠端庫

需要将github.com修改成ggbondd(配置檔案中的Host)

git remote add fp git@ggbondd:ggbondd/fucking-physical.git

git branch -M main

git push -u fp main

git remote add origin git@ggbond6:GGBond6/typora.git

git push -u origin main