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

参考:

相关推荐
就叫飞六吧7 分钟前
git提取当前分支指定文件历史版本
git
数字游民95277 分钟前
网站备案全流程回放(腾讯云)
人工智能·git·github·腾讯云·网站备案·waytoopc
Albert Edison41 分钟前
【Git】多人协作一(同一分支下)
git·vscode·svn·github
学好statistics和DS1 小时前
Git 同步冲突
大数据·git·elasticsearch
德彪稳坐倒骑驴14 小时前
Git常用命令
git
无限进步_14 小时前
【C语言&数据结构】对称二叉树:镜像世界的递归探索
c语言·开发语言·数据结构·c++·git·算法·visual studio
qq_54702617917 小时前
Git 使用指南
git
XiaoHu020718 小时前
Linux多线程(详细全解)
linux·运维·服务器·开发语言·c++·git
*才华有限公司*19 小时前
RTSP视频流播放系统
java·git·websocket·网络协议·信息与通信
juelianhuayao20 小时前
Git错误提交后如何快速删除本次commit
git