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 小时前
4 Git远程协作:从零开始,玩转仓库关联与代码同步(带实操代码讲解)
大数据·c++·git·elasticsearch·搜索引擎·gitee·github
金銀銅鐵11 小时前
[git] 浅解 git reset 命令
git·后端
zhangfeng113312 小时前
部署到服务器上 宝塔系统 使用宝塔在线编辑器 FTP 批量上传 Git 部署 打包上传 codebudyy 编程程序开发
服务器·git·编辑器
学习是种信仰12 小时前
Git工作流
git·深度学习
yuanyuan2o214 小时前
Git merge 的几种不同模式
git·github
视觉小萌新14 小时前
关于Vscode配置企业Git
git
zh_xuan15 小时前
使用命令行把安装包上传到github
c++·git·libcurl·c++工程打包
zhangfeng113316 小时前
适合 5人以内小团队的Git 工作流 + Code Review + 自动化部署方案 FastAdmin +linunx服务器宝塔系统 外包项目 —
服务器·git·自动化·php·代码复审
念一不念二21 小时前
[Git]git仓库管理的使用
git