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

参考:

相关推荐
wdfk_prog6 小时前
实战指南:如何将Git仓库中的特定文件夹及其历史完整迁移到另一个仓库
大数据·linux·运维·笔记·git·学习·elasticsearch
乌萨奇也要立志学C++14 小时前
【Linux】linux基础开发工具(三) 版本控制器Git、调试器 - gdb/cgdb使用、一些实用的调试技巧
linux·git·elasticsearch
Doris_LMS16 小时前
Git、Gitee、GitHub、GitLab完整讲解:从基础到进阶
git·gitee·gitlab·github
CV资深专家1 天前
常用git命令
git
bug和崩溃我都要1 天前
Git提交代码完整流程
git
逢考必过@k1 天前
git的使用
git
狂炫一碗大米饭1 天前
如何在 Git 中检出远程分支
前端·git·github
dolzhuying1 天前
git常见场景食用指南
git
上邪o_O1 天前
Git 的基本使用指南(1)
linux·git
飏旎2 天前
git pull和git fetch的区别
git