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...

相关推荐
地球没有花9 小时前
gitlab cicd首次操作
运维·git·ci/cd·gitlab
无限进步_18 小时前
【C语言】贪吃蛇游戏设计思路深度解析:从零开始理解每个模块
c语言·开发语言·c++·git·游戏·github·visual studio
达子66620 小时前
git使用应用实战大全
git
P***25391 天前
Git教程
git
Slow菜鸟1 天前
Java 开发环境安装指南(五) | Git 安装
java·git
计算衎1 天前
Git 命令 作用、常用选项、示例、何时使用与注意事项指南
git·源代码管理
小生不才yz1 天前
(三)分支与合并 - git cherry-pick 命令的使用
git
hadage2332 天前
--- git 笔记 ---
笔记·git·elasticsearch
johnny2332 天前
Git:进阶、衍生
git
草莓熊Lotso2 天前
Git 本地操作入门:版本控制基础、跨平台部署与仓库核心流程
开发语言·人工智能·经验分享·git·后端·架构·gitee