Try   HackMD

多個專案之GitHub SSH key密鑰的管理

Sharon Mai20230515, Mon

↪️總目錄:Git and GitHub相關

情境:當有多個Github上儲存的專案,需要使用到不同的密鑰管理。

創建密鑰

  1. 在根目錄進入.ssh/ 資料夾,若無則創建一個.ssh/ 資料夾

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  2. 創建SSH Key,使用ed25519類型創建

    1. 打開終端機或右鍵點選Git Bash Here

      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →

    2. 輸入指令:

      ​​​​​​​​ssh-keygen -t ed25519 -C <此處輸入電子信箱>
      
    3. 完整示範指令如下:

      ​​​​​​​​ssh-keygen -t ed25519 -C xxx@gmail.com
      
  3. Github密鑰其餘步驟請參考:Ubuntu部署Django 4專案系列的公鑰設定

密鑰管理

使用設定檔

  1. 到 .ssh/ 資料夾內

  2. 創建名稱叫config的文件,並輸入以下內容:

    留意Host 別名,它是下個步驟的設定關鍵。

    ​​​​# github_專案A
    ​​​​Host github_project_A   # Host 別名。能認得即可
    ​​​​  HostName github.com   # 儲存的網站
    ​​​​  PubkeyAcceptedAlgorithms ssh-ed25519  # 不動
    ​​​​  IdentityFile ~/.ssh/id_ed25519   # 路徑位置
    
    ​​​​# github_專案B
    ​​​​Host github_project_B
    ​​​​    HostName github.com
    ​​​​    PubkeyAcceptedAlgorithms ssh-ed25519
    ​​​​    IdentityFile ~/.ssh/prj_Django
    
  3. 完成資料內容如下圖:

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

Clone專案

使用Host別名分別開來

  1. 到想Clone的資料夾中
  2. 打開終端機或直接右鍵點選Git Bash Here
  3. 輸入指令
    1. 先到GitHub複製連結

      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →

    2. 在指令中貼上GitHub複製的連結:

      ​​​​​​​​git clone git@github.com:MaiSharon/prj_django.git
      
    3. 調整如下:

      留意Host 別名。

      ​​​​​​​​git clone git@<Host 別名>:MaiSharon/prj_django.git
      
    4. 完整示範指令如下:

      留意Host 別名。

      ​​​​​​​​git clone git@<github_project_A>:MaiSharon/prj_django.git
      
    5. 成功如下圖🎉:

      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →