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

参考:

相关推荐
计算衎6 小时前
git仓库中的.git目录 , .gitattributes、.gitignore、.gitmodules、.modules文件作用与讲解
git·git bash·文件结构
Murphy_lx10 小时前
git工作流程
git
Elias不吃糖10 小时前
Git常用指令合集
linux·git
melt_102612 小时前
【gitee账号设置】git多个账号在多台电脑上使用
git·gitee·git账号管理
南宫真汀14 小时前
微信小程序项目上传到git仓库(完整操作)
git
夜月yeyue15 小时前
嵌入式开发中的 Git CI/CD
c++·git·单片机·嵌入式硬件·ci/cd·硬件架构
真人不梦16 小时前
Lazygit: 从0到熟练使用,你需要的都在这里
git·github
denggun1234521 小时前
图片上传git时压缩
git·ios·objective-c·iphone·xcode
行走的陀螺仪1 天前
git-旧项目继续开发新功能,同时还要维护线上版本
git·git push·git commit·git 协同开发
fendouweiqian1 天前
git提交与commitlint规则
git