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

参考:

相关推荐
lichong9519 小时前
Git 检出到HEAD 再修改提交commit 会消失解决方案
java·前端·git·python·github·大前端·大前端++
222you11 小时前
Git仓库推送到GitHub
git·github
你的人类朋友13 小时前
hotfix分支的使用
git·gitlab·github
小蜜蜂爱编程14 小时前
gerrit的部署与配置关联到不同服务器上的git仓库
运维·服务器·git·gerrit
颇有几分姿色16 小时前
Git将本地项目推送到GitLab
git·gitlab
Kent_J_Truman20 小时前
Git个人配置偏好记录以及注意事项
git
小龙报1 天前
《算法每日一题(1)--- 连续因子》
c语言·开发语言·c++·windows·git·算法·visual studio
今禾1 天前
Git完全指南(中篇):GitHub团队协作实战
前端·git·github
flow_code2 天前
切换git账户
git
春生野草2 天前
Gituee
git·gitee