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

参考:

相关推荐
LannyChung8 小时前
PowerShell 下 git log 中文乱码的完整排查与解决
windows·git
import_random9 小时前
[git]本地main,origin/main,远程main(关系2)
git
golang学习记9 小时前
Zed Git重大更新:目录级对比支持分栏Diff
git·vscode·zed
阿俊-全栈开发14 小时前
LikeShop 单商户高级版二开项目的 Git 分支管理与版本发布规范
git·likeshop·likeshop开源商城·likeshop多商户
努力弹琴的大风天14 小时前
TortoiseGit 仓库不显示绿色对号解决方案
git·tortoisegit·图标显示
我不会插花弄玉17 小时前
GitHub实操【由浅入深-Git】
git·github
说实话起个名字真难啊17 小时前
Git常用小问题笔记
笔记·git·elasticsearch
橙露1 天前
git简明教程以及资源分享
大数据·git
codigger2 天前
别再靠"最终版-改"活下去了:一篇讲透 Git 与 GitHub
git·程序员·github·编程·控制版本
JustNow_Man2 天前
【Harness】本地文本向量化搜索解决方案--qmd
git·python·算法