<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          本地如何配置多個 GitHub/Gitee 賬號?

          共 2109字,需瀏覽 5分鐘

           ·

          2022-01-22 10:27

          閱讀本文大概需要 5 分鐘。

          大家好,我是 polarisxu。

          現在的開發(fā),無論是日常工作還是參與開源,都離不開 Git。開源項目,大家通常使用 GitHub 或 Gitee,而工作中通常會自建 Git 服務,比如通過 GitLab、Gogs 等搭建。

          為了方便使用,一般大家會配置 SSH keys,通過 ssh 協議 pull/push 倉庫。

          1、生成 ssh 密鑰

          首先,我們需要生成 ssh 密鑰:(基于 mac,linux 類似,Windows 下找對應工具)

          ssh-keygen?-C?"[email protected]"
          Generating?public/private?rsa?key?pair.
          Enter?file?in?which?to?save?the?key?(/Users/xuxinhua/.ssh/id_rsa):

          出現的提示,使用默認值即可。命令執(zhí)行完后,會生成 id_rsa 和 id_rsa.pub 文件,其中 id_rsa.pub 是公鑰,拷貝其中的內容配置到 GitHub 或 GitLab 之類的網站。比如 GitHub 是這里:https://github.com/settings/ssh/new。

          2、一個電腦兩個不同網站賬號

          這是最常見的場景:一個業(yè)余號(github),一個工作號(比如自建 gitlab)。因為是不同網站,因此可以使用同一個郵箱。當然也可以是一個 github 賬號,一個 gitee 賬號,為了方便,以下使用 github 和 gitee。

          ~/.ssh 目錄下創(chuàng)建一個 config 文件,在其中添加如下內容:

          host github
          hostname github.com
          Port 22
          host gitee
          hostname gitee.com
          Port 22

          這里沒有指定 id_rsa,因為默認讀取的就是它。

          這樣,本地使用 GitHub 還是 Gitee 完全沒區(qū)別。

          注意,需要使用 id_rsa.pub 分別在 GitHub 和 Gitee 添加 SSH Keys

          當然,你也完全可以使用兩個不同的賬號,具體見下文。

          3、一個電腦兩個 GitHub 賬號

          因為兩個 GitHub 賬號,自然不能使用同一個 ssh 密鑰,因此生成另外一個:

          $?ssh-keygen?-t?rsa?-f?~/.ssh/id_rsa_gmail?-C?"[email protected]"

          這會在 ~/.ssh 目錄生成 id_rsa_gmail 和 id_rsa_gmail.pub 兩個文件。

          將 id_rsa.pub 和 id_rsa_gmail.pub 配置到對應的 GitHub 賬號。然后跟上文一樣,編輯 config 文件:

          # github 賬號:[email protected]
          host?github
          ????hostname?github.com
          ????Port?22
          ????User?git
          ????IdentityFile?~/.ssh/id_rsa
          # github 賬號:[email protected]
          host?gmail-github
          ????hostname?github.com
          ????Port?22
          ????User?git
          ????IdentityFile?~/.ssh/id_rsa_gmail

          config 是 ssh 的配置,詳細信息可以參考:https://daemon369.github.io/ssh/2015/03/21/using-ssh-config-file。

          針對以上場景,在具體使用時,我們需要注意以下幾點:

          • 默認會使用第一個賬號,要使用第二個賬號,需要設置該項目自己的 user.email 和 user.name
          • git clone 時,第二個賬號,地址得是類似這樣的:[email protected]:studygolang/studygolang.git

          如果有問題,可以執(zhí)行以下兩個命令驗證:(記得替換為你自己的配置)

          $?ssh-add?~/.ssh/id_rsa_gmail
          [email protected]

          4、總結

          生活一個號,工作一個號。如果你沒有很好的區(qū)分,可以試試本文的方法,更愉快的 Coding!




          往期推薦


          我是 polarisxu,北大碩士畢業(yè),曾在 360 等知名互聯網公司工作,10多年技術研發(fā)與架構經驗!2012 年接觸 Go 語言并創(chuàng)建了 Go 語言中文網!著有《Go語言編程之旅》、開源圖書《Go語言標準庫》等。


          堅持輸出技術(包括 Go、Rust 等技術)、職場心得和創(chuàng)業(yè)感悟!歡迎關注「polarisxu」一起成長!也歡迎加我微信好友交流:gopherstudio


          瀏覽 107
          點贊
          1評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          全部評論
          QS220966a9de7ba74e12024-07-31 14:50
          “git clone 時,第二個賬號,地址得是類似這樣的:[email protected]:studygolang/studygolang.git”這里的地址應該是“gmail-github:studygolang/studygolang.git”
          點贊回復
          推薦
          點贊
          1評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产播放一区二区在线玩弄 | www.91一区二区 | 影音先锋 一区二区三区 | 免费操视频 | 青草青视频在线 |