首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Git 使用者名稱、郵箱與 SSH Key 關係:一台電腦可設定多組身份

     編輯:程式設計綜合問答

核心概念

  • user.name / user.email:**提交作者身份**,記錄在每個 commit 物件中,**與 SSH Key 無直接綁定**。
  • SSH Key:**網路傳輸認證**,證明「你擁有私鑰」,GitHub/GitLab 依公鑰辨識**帳號權限**。
  • **一台電腦/一個系統用戶** 可擁有 **多組 SSH Key** (如 `id_ed25519_work`、`id_ed25519_personal`),對應不同 Git 平台帳號。

多身份配置最佳實踐

  1. 產生多組金鑰:
    
    ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_work
    ssh-keygen -t ed25519 -C "personal@gmail.com" -f ~/.ssh/id_ed25519_personal
    
  2. 配置 `~/.ssh/config`** 指定不同 Host 使用不同金鑰:
    
    # 公司 GitLab
    Host gitlab.company.com
        HostName gitlab.company.com
        User git
        IdentityFile ~/.ssh/id_ed25519_work
        IdentitiesOnly yes
    
    # 個人 GitHub
    Host github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519_personal
        IdentitiesOnly yes
    
  3. 專案層級設定 user.name/email** (優先於全域):
    
    cd ~/projects/company-repo
    git config user.name "Work Name"
    git config user.email "work@company.com"
    
    cd ~/projects/personal-repo
    git config user.name "Personal Name"
    git config user.email "personal@gmail.com"
    

常見誤區澄清

誤區事實
一台電腦只能有一個 user.name/email**全域設定** (`git config --global`) 只有一組,但**每個專案可獨立覆蓋** (`git config` 不加 `--global`)。
一個 SSH Key 只能對應一個倉庫**一個 SSH Key 對應一個 Git 平台帳號**,該帳號下**所有有權限的倉庫**皆可存取。
Windows 每個用戶只能一組 SSH Key同一用戶可建立多組金鑰檔,透過 `config` 指定。
專案目錄必須在 Git 安裝目錄下**專案可放在任何位置**,`git init` 即可建立倉庫,與安裝目錄無關。

工作流建議

  1. 專案根目錄建立 `.gitconfig` (或 `.git/config` 已含) 設定專案專用身份。
  2. 使用 **Git Credential Manager** (Windows/Mac) 或 **SSH Agent** 自動管理金鑰解鎖。
  3. 定期 `git config --list --show-origin` 檢查生效配置來源。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved