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

参考:

相关推荐
naice16 小时前
我对github的图片很不爽了,于是用AI写了一个图片预览插件
前端·javascript·git
会飞的青蛙1 天前
GIT 配置别名&脚本自动化执行
前端·git
权咚3 天前
阿权的开发经验小集
git·ios·xcode
nightunderblackcat4 天前
新手向:Git下载全攻略
git
若无_4 天前
了解 .husky:前端项目中的 Git Hooks 工具
前端·git
想当花匠的小码农4 天前
Git 仓库“减肥”指南:从检测、清理到同步的全流程实践
git
R_.L4 天前
Git : 基本操作
git
python_1365 天前
git常见冲突场景及解决办法
git
洛小豆5 天前
Git 打标签完全指南:从本地创建到远端推送
前端·git·github