Git 凭据管理器原理与多账号管理指南
一、Git 凭据管理器原理
1.1 什么是 Git 凭据管理器
Git 凭据管理器(Git Credential Manager)是 Git 用来存储和管理用户名密码的工具,避免每次操作都需要输入凭据。
1.2 凭据存储机制
Windows 系统上,Git 主要使用以下方式存储凭据:
- Git Credential Manager Core (GCM Core) :现代推荐方式,将凭据加密存储在 Windows 凭据管理器中
- Store 方式 :以明文形式存储在
~/.git-credentials
文件中(不安全) - Cache 方式:在内存中临时缓存,一段时间后失效
1.3 凭据匹配规则
Git 凭据管理器根据以下信息来匹配和存储凭据:
ruby
协议://用户名@域名
关键点:
- 同一个域名下,默认只能存储一套凭据
- 如果 URL 中包含用户名,则会为每个用户名单独存储凭据
- 凭据的匹配优先级:
协议 + 域名 + 用户名
>协议 + 域名
示例:
perl
https://codeup.aliyun.com → 存储一套凭据(任何用户)
https://user1@codeup.aliyun.com → 存储 user1 的凭据
https://user2@codeup.aliyun.com → 存储 user2 的凭据
1.4 工作流程
markdown
1. 用户执行 git clone/pull/push
↓
2. Git 检查是否需要身份验证
↓
3. Git 查询凭据管理器是否有匹配的凭据
↓
4. 如果有 → 自动使用
如果没有 → 提示用户输入
↓
5. 用户输入后,凭据管理器保存凭据
↓
6. 下次自动使用已保存的凭据
二、常见问题:同域名多账号冲突
2.1 问题场景
当你在同一个 Git 托管平台(如阿里云 CodeUp)下有多个账号和仓库时:
bash
仓库1:https://codeup.aliyun.com/project1/repo1.git (用户 user1)
仓库2:https://codeup.aliyun.com/project2/repo2.git (用户 user2)
由于域名相同,Git 默认只会保存一套凭据,导致:
- 第一个仓库操作正常
- 第二个仓库使用第一个账号的凭据,认证失败
2.2 解决方案
在 URL 中指定用户名,让 Git 为不同用户分别存储凭据:
bash
# 正确的 clone 方式
git clone https://user1@codeup.aliyun.com/project1/repo1.git
git clone https://user2@codeup.aliyun.com/project2/repo2.git
2.3 已有仓库的修改方法
如果仓库已经 clone 下来,需要修改 remote URL:
bash
# 进入仓库目录
cd your-repo
# 查看当前 remote URL
git remote -v
# 修改 remote URL,添加用户名
git remote set-url origin https://用户名@codeup.aliyun.com/your-repo.git
# 验证修改
git remote -v
修改后的效果:
- 下次 pull/push 会提示输入该用户名的密码
- 输入后凭据管理器会保存
- 之后所有操作自动使用对应凭据
三、查看和管理凭据
3.1 查看所有 Git 凭据(PowerShell)
bash
# 查看所有 Git 相关凭据
cmdkey /list | Select-String "git:"
输出示例:
perl
Target: git:https://github.com
Target: git:https://user1@codeup.aliyun.com
Target: git:https://user2@codeup.aliyun.com
3.2 查看凭据详细信息
方法1:Windows 凭据管理器(图形界面)
bash
# 打开凭据管理器
Win + R → control /name Microsoft.CredentialManager
# 或命令行打开
rundll32.exe keymgr.dll,KRShowKeyMgr
在 Windows 凭据 中查看所有 git:
开头的凭据。
方法2:Git Credential Manager 命令
bash
# 列出所有凭据
git credential-manager-core list
# 或简写
git-credential-manager-core list
3.3 删除凭据
方法1:使用 Git Credential Manager
perl
# 删除特定域名的凭据
git credential-manager-core delete https://codeup.aliyun.com
# 删除特定用户的凭据
git credential-manager-core delete https://user1@codeup.aliyun.com
方法2:使用 cmdkey 命令
ruby
# 删除凭据(需要完整的目标名称)
cmdkey /delete:git:https://codeup.aliyun.com
cmdkey /delete:git:https://user1@codeup.aliyun.com
方法3:在凭据管理器中手动删除
打开凭据管理器 → 找到对应凭据 → 点击删除
四、完整操作流程示例
场景:在阿里云 CodeUp 上管理两个不同账号的仓库
步骤1:查看现有凭据
vbnet
cmdkey /list | Select-String "git:"
步骤2:删除冲突的凭据
perl
# 如果发现有不带用户名的凭据,删除它
git credential-manager-core delete https://codeup.aliyun.com
步骤3:使用带用户名的 URL 进行 clone
bash
# Clone 第一个账号的仓库
git clone https://user1@codeup.aliyun.com/project1/repo1.git
# Clone 第二个账号的仓库
git clone https://user2@codeup.aliyun.com/project2/repo2.git
步骤4:首次操作时输入密码
- 第一次 pull/push 时会提示输入密码
- 输入后凭据管理器自动保存
- 后续操作自动使用对应凭据
步骤5:验证配置
shell
cd repo1
git remote -v
# 应显示:origin https://user1@codeup.aliyun.com/project1/repo1.git
cd ../repo2
git remote -v
# 应显示:origin https://user2@codeup.aliyun.com/project2/repo2.git
步骤6:正常使用
perl
# 在 repo1 中
git pull # 自动使用 user1 的凭据
git push # 自动使用 user1 的凭据
# 在 repo2 中
git pull # 自动使用 user2 的凭据
git push # 自动使用 user2 的凭据
五、高级技巧
5.1 查看当前仓库使用的凭据配置
csharp
# 查看 remote URL
git remote -v
# 查看凭据助手配置
git config --get credential.helper
5.2 临时禁用凭据管理器
bash
# 临时不使用凭据管理器(每次都输入密码)
git -c credential.helper= clone https://codeup.aliyun.com/your-repo.git
5.3 为特定仓库配置独立的凭据助手
bash
cd your-repo
# 为当前仓库单独配置凭据存储方式
git config credential.helper store
# 这会在仓库目录下创建 .git-credentials 文件
5.4 使用 SSH 方式(推荐长期使用)
如果需要频繁管理多个账号,建议使用 SSH 密钥方式:
bash
# 为每个账号生成独立的 SSH 密钥
ssh-keygen -t rsa -C "user1@email.com" -f ~/.ssh/id_rsa_codeup_user1
ssh-keygen -t rsa -C "user2@email.com" -f ~/.ssh/id_rsa_codeup_user2
# 配置 ~/.ssh/config
# Host codeup-user1
# HostName codeup.aliyun.com
# User git
# IdentityFile ~/.ssh/id_rsa_codeup_user1
#
# Host codeup-user2
# HostName codeup.aliyun.com
# User git
# IdentityFile ~/.ssh/id_rsa_codeup_user2
# 使用 SSH 方式 clone
git clone git@codeup-user1:project1/repo1.git
git clone git@codeup-user2:project2/repo2.git
六、常用命令速查表
| 操作 | 命令 |
|---------------|--------------------------------------------------------------|------------------------|
| 查看所有凭据 | `cmdkey /list | Select-String "git:"` |
| 查看凭据详情 | git credential-manager-core list
|
| 删除特定凭据 | git credential-manager-core delete https://domain.com
|
| 打开凭据管理器 | control /name Microsoft.CredentialManager
|
| 查看 remote URL | git remote -v
|
| 修改 remote URL | git remote set-url origin https://user@domain.com/repo.git
|
| 查看凭据配置 | git config --get credential.helper
|
| Clone 时指定用户 | git clone https://username@domain.com/repo.git
|