git配置(1): 根据remote自动选择账号执行commit

git配置(1): 根据remote自动选择账号执行commit

在公司电脑上, 克隆了 github 的仓库, 也克隆了内网 gitlab 的仓库。

希望 commit 和 push 到内网 gitlab 时, 使用公司账号; commit 和 push 到 github 时, 使用个人账号。

原本以为 ~/.gitconfig 只能配置单个账户:

复制代码
[user]
    name = my_personal_name
    email = my_personal_email@foxmail.com    

发现 git 2.45 版本可以根据正则匹配到 remote 或 branch, 来载入特定的配置文件; 特定配置文件中使用公司账号即可:

https://git-scm.com/docs/git-config#_includes

bash 复制代码
[includeIf "hasconfig:remote.*.url:https://*.my_company.com/**"]
    path = .gitconfig-my_company

.gitconfig-my_company 内容:

复制代码
[user]
    name = my_account_at_company
    email = my_account_at_company@my_company.com

ubuntu-22.04 默认 git 版本不识别上述语法, 需要加 ppa 源更新 git:

bash 复制代码
sudo add-apt-repository -y ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git -y

参考:

相关推荐
影视飓风TIM13 小时前
Linux下C语言缓冲区原理 + Git版本控制
linux·c语言·git
tianyuanwo19 小时前
当提交标题重复阻塞CI:Git分支修复实战
git·ci/cd·分支基线调整
Salt & Light19 小时前
Git 分支操作与恢复完整记录
git
重生的黑客19 小时前
从远程仓库到企业级协作:Git push、pull、PR、多人开发与分支模型
git·分支·多人协作
tianyu2341 天前
Git 常用命令完整手册
git·版本控制·常用命令
gwf2162 天前
SSD读写速度深度解析:顺序读写vs随机读写、IOPS、延迟,你的硬盘性能到底怎么看?
git·嵌入式硬件·缓存·github·智能硬件
西邮彭于晏2 天前
图文详解:Git分支创建、合并与冲突解决|新手零门槛完整教程
大数据·git·elasticsearch
nuisthou2 天前
git常用命令总结
git
西邮彭于晏2 天前
Git 标签(Tag)与版本发布完整指南|附全场景命令速查表
大数据·git·elasticsearch
潘正翔2 天前
k8s进阶_Harbor镜像仓库
git·云原生·容器·kubernetes·gitee·github