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

相关推荐
悠然大月季11 小时前
git 怎么导出提交历史,文件是乱码
git·git导出历史记录·git导出历史乱码
chenshiming80212 小时前
在cursor下执行GIT回退版本
git
打点计时器15 小时前
Git快速上手教程
git
我才是一卓16 小时前
linux 安装简易 git 服务端并使用
linux·运维·git
IDIOT___IDIOT18 小时前
关于 git 进行版本管理的时候 gitignore 写入忽略规则而不生效的问题
大数据·git·elasticsearch
不想看见40418 小时前
Git 误删急救手册
大数据·git·elasticsearch
偷懒下载原神18 小时前
【linux操作系统】信号
linux·运维·服务器·开发语言·c++·git·后端
IT二叔18 小时前
Git Flow03-发布流程
git
IT二叔18 小时前
Git Flow08-摘樱桃
git
「QT(C++)开发工程师」19 小时前
Git误操作急救手册大纲
git