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

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

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

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

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

复制代码
[user]
    name = my_personal_name
    email = [email protected]    

发现 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

参考:

相关推荐
A.A呐7 小时前
【Linux第四章】gcc、makefile、git、GDB
linux·c语言·开发语言·c++·git
love530love12 小时前
【新手向】GitHub Desktop 的使用说明(含 GitHub Desktop 和 Git 的功能对比)
人工智能·windows·git·python·github
学IT的小卢13 小时前
获取 Git 仓库
git
范纹杉想快点毕业19 小时前
Qt、C++自定义按钮、组件、事件编程开发练习,万字实战解析!!
java·c语言·开发语言·c++·git·qt·github
stark张宇21 小时前
Centos7 安装部署Git服务、创建仓库
git·后端
唯梦闲君21 小时前
windows中通过git从远程快速只下载指定一个文件的批处理命令
windows·git
Two_brushes.1 天前
Git 实战场景
git
巴里巴气1 天前
git常用操作 --- idea编译器 --- 公司实战版
git·idea
炽天使3281 天前
Git 日常操作流程(备查)
git·学习
程序员王天2 天前
Git Push 报错图解:从分支分叉到代码恢复
前端·git