git配置多个用户

利用 .gitconfigincludeIf 语句匹配不同的远程仓库url切换 user

配置 gitconfig

~/.gitconfig 同目录下创建多个 .gitconfig.<你希望的名称> 配置文件。

在配置文件里写入以下内容:

txt 复制代码
[user]
    name = <your name>
    email = <your email>

以github 和 gitee 举例,我希望在这两个 remote 中提交代码使用不同的 user(真实场景应该是公司内部仓库应该使用真实名字提交)

txt 复制代码
[includeIf "hasconfig:remote.*.url:git@github.com:**/**/"]
	path = ~/.gitconfig.<你希望的名称>
[includeIf "hasconfig:remote.*.url:*gitee.com**/**/*"]
	path = ~/.gitconfig_gitee.<你希望的名称2>

如果习惯使用 http 链接拉取仓库,则需要这样写:

txt 复制代码
[includeIf "hasconfig:remote.*.url:https://github.com/**"]
	path = ~/.gitconfig.<你希望的名称>
[includeIf "hasconfig:remote.*.url:https://gitee.com/**"]
	path = ~/.gitconfig_gitee.<你希望的名称2>

当然也可以同时把这几条都写上去。includeIf 不支持条件或之类的语法,只能一条一条写。

进入到对应remote的代码仓库中,使用以下命令可以确认配置是否生效:

bash 复制代码
git config user.name # 输出对应的user则代表成功

注意事项

了解过 gitconfig 文件的语法后,你可能会希望这样写:

txt 复制代码
[includeIf "hasconfig:remote.*.url:git@github.com:**/*"]
    [user]
        name = <your name>
        email = <your email>
        
[includeIf "hasconfig:remote.*.url:*gitee.com**/*"]
    [user]
        name = <your name2>
        email = <your email2>

这是不行的哦,会默认使用最后一个 user。

相关文档

git-scm.com/docs/git-co...

相关推荐
阿正的梦工坊3 小时前
Git提交中的perf和chore是什么?
git
Fly feng3 小时前
git rebase 变基操作教程
git
laoliu19963 小时前
开源 FRP GUI 客户端
git·开源·github
YanaDH5 小时前
前端开发新机环境部署(带链接)
git·vscode·npm·node.js
微尘hjx6 小时前
【GitHub 代码仓 03】Git + GitHub 实战速查手册
git·github
艺杯羹20 小时前
Git版本控制深度复盘:从入门到精通的完整指南
git·wpf·版本控制·git学习·git复盘
菩提小狗21 小时前
第7天:信息打点-资产泄漏&CMS识别&Git监控&SVN&DS_Store&备份|【小迪安全】web安全|渗透测试|网络安全-2021
git·安全·svn
Ghost Face...1 天前
嵌入式Linux开发Git实战:从认证到Gerrit推送
linux·git·elasticsearch
LeoZY_1 天前
开源项目精选: lazygit —— 告别繁琐命令,终端里玩转可视化Git
git·stm32·单片机·mcu·开源·远程工作·gitcode
黎潇lulu1 天前
IDEA的Git使用方法(中文IDEA版)
java·git·intellij-idea