在配置 电脑支持 git 多账号 之后, 由于设置的 name 和 email 是全局的, 所以不同项目提交的时候用户名是相同的, 这严重违背了隔离的原则.
删除全局配置, 注释掉
email
和name
内容1
2
3
4
5
6
7
8
9# 删除全局配置 name 和 email
> vim ~/.gitconfig
[user]
useConfigOnly = true
# name = admin
# email = admin@gmail.com
# 查看配置是否生效
> git config --global user.name && git config --global user.email在每个 git 项目中单独配置, 这样可以实现各个项目的提交人不同
1
2
3
4
5
6# 配置基于项目的 name 和 email
> git config user.name "zs"
> git config user.email "zs@qq.com"
# 查看配置是否生效
> git config --local user.name && git config --local user.email
而且由于删除了全局配置, 在每次提交代码的时候, 都会提示没有设置 name
和 email
, 这样会提交失败, 保证提醒针对项目, 设置正确的 name 和 email.
详细解析
在使用多个 .ssh/config 配置指向不同 GitHub 账户时,为了让不同项目使用不同的 email 和 name,需要为每个 Git 仓库单独配置用户信息,同时确保 SSH 配置正确映射到对应的 GitHub 账户。以下是详细的解决方案:
1. 配置 .ssh/config 文件
确保你的 .ssh/config 文件已经正确配置了多个 GitHub 账户的 SSH 密钥。例如:
1 | # 账户 1 (例如 personal GitHub) |
- Host 是你自定义的别名,用于区分不同的 GitHub 账户。
- IdentityFile 指向对应的私钥文件(如 ~/.ssh/id_rsa_personal 和 ~/.ssh/id_rsa_work)。
确保这些私钥已经添加到你的 SSH 代理 (ssh-add ~/.ssh/id_rsa_personal 和 ssh-add ~/.ssh/id_rsa_work)。
2. 为每个 Git 仓库设置独立的 user.name 和 user.email
Git 允许在每个仓库中设置独立的 user.name 和 user.email,这些配置会覆盖全局配置。操作步骤如下:
进入项目目录: 假设你有两个项目,分别对应不同的 GitHub 账户:
- 项目 A:~/projects/personal-project(使用 github.com-personal)
- 项目 B:~/projects/work-project(使用 github.com-work)
为项目 A 设置用户信息
进入项目 A 的目录并运行以下命令:
1
2
3cd ~/projects/personal-project
git config user.name "YourPersonalName"
git config user.email "personal@example.com"为项目 B 设置用户信息
进入项目 B 的目录并运行以下命令:
1
2
3cd ~/projects/work-project
git config user.name "YourWorkName"
git config user.email "work@example.com"验证配置
在每个项目目录下运行以下命令,确认配置是否正确:
1
2git config --get user.name
git config --get user.email这些配置会保存在每个项目目录下的 .git/config 文件中,适用于该项目的所有 Git 操作。
3. 配置 Git 仓库的远程 URL 使用正确的 SSH 别名
为了确保 Git 使用正确的 SSH 配置(即 .ssh/config 中的 Host),需要将每个项目的远程仓库 URL 设置为对应的 Host 别名。
检查当前远程 URL
在项目目录下运行:
1
git remote -v
如果远程 URL 是标准的
git@github.com:username/repo.git
,需要将其修改为使用.ssh/config
中的别名。修改远程 URL
对于项目 A(使用 github.com-personal):
1
git remote set-url origin git@github.com-personal:username/repo.git
对于项目 B(使用 github.com-work):
1
git remote set-url origin git@github.com-work:username/repo.git
这里 username/repo 应替换为实际的 GitHub 用户名和仓库名。
验证 SSH 连接
确保 SSH 配置正确,运行以下命令测试连接:
1
2ssh -T git@github.com-personal
ssh -T git@github.com-work如果配置正确,你应该会看到类似
Hi username! You've successfully authenticated...
的消息。
4. 提交代码
在每个项目目录下,提交代码时 Git 会自动使用该项目目录下配置的 user.name
和 user.email
,并通过 .ssh/config
中定义的 SSH 别名连接到正确的 GitHub 账户。
例如,在 personal-project 目录下:
1 | git add . |
提交会使用 YourPersonalName
和 personal@example.com
,并通过 github.com-personal
的 SSH 密钥推送。
5. 可选:设置条件包含(Conditional Includes)
如果你的项目分布在不同的目录,可以使用 Git 的条件包含(Conditional Includes)来自动加载对应的全局配置,减少手动设置的麻烦。
创建单独的 Git 配置文件
在 ~/.gitconfig
中配置条件包含。例如:
1 | # 主 Git 配置文件 (~/.gitconfig) |
创建个人和工作配置文件
~/.gitconfig-personal
:
1 | [user] |
~/.gitconfig-work
:
1 | [user] |
工作原理
当你在 ~/projects/personal/
下的仓库操作时,Git 会自动加载 ~/.gitconfig-personal
中的配置。
当你在 ~/projects/work/
下的仓库操作时,Git 会加载 ~/.gitconfig-work
。
注意:includeIf 仅适用于 Git 2.13 及以上版本。运行 git –version 检查你的 Git 版本。
6. 注意事项
SSH 密钥管理:确保你的 SSH 密钥已正确生成并添加到 GitHub 账户(在 GitHub 的 Settings > SSH and GPG keys 中添加)。
全局配置:如果你没有为某个仓库设置 user.name 和 user.email,Git 会使用全局配置(~/.gitconfig)。建议检查全局配置以避免意外:
1
2git config --global user.name
git config --global user.emailSSH 代理:确保 SSH 代理已加载所有必要的密钥:
1
ssh-add -l
如果密钥未列出,添加它们:
1 | ssh-add ~/.ssh/id_rsa_personal |
多个远程仓库:如果一个项目需要推送到多个 GitHub 账户,可以添加多个远程 URL(例如 origin-personal
和 origin-work
),并分别推送到不同的远程。
7. 故障排查
- 提交显示错误的用户名:检查当前仓库的
git config user.name
和user.email
,确保它们与目标 GitHub 账户一致。 - SSH 连接失败:运行
ssh -vT git@github.com-personal
检查 SSH 连接问题,可能需要检查密钥权限(chmod 600 ~/.ssh/id_rsa*
)。 - 全局配置干扰:如果全局配置覆盖了本地配置,运行
git config --unset user.name
和git config --unset user.email
清除本地配置,重新设置。
通过以上步骤,你可以在不同项目中使用不同的 user.name
和 user.email
,并通过 .ssh/config
正确映射到对应的 GitHub 账户。