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

参考:

相关推荐
化作繁星7 小时前
git忽略校验,强制提交
git
爱宇阳9 小时前
Git 教程 | 如何将指定文件夹回滚到上一次或某次提交状态(命令详解)
git·git回滚
杰哥技术分享11 小时前
Git 中移除已追踪的文件
git
暗影~行星13 小时前
VS Code 插件 Git History Diff
git
lee57613 小时前
Git 克隆别人的远程仓库以后,推到自己的远程仓库
git
山海上的风13 小时前
idea本地git上传gitee码云失败分析,push rejected+git手动融合
git·gitee·intellij-idea
遇见火星19 小时前
Ansible模块——Ansible配置文件!
linux·git·ansible
意倾城1 天前
Git的三种合并方式
git
卖猪肉的痴汉2 天前
切换到旧提交,同时保证当前修改不丢失
git
五碗火锅2 天前
【pycharm】如何连接远程仓库进行版本管理(应用版本)
ide·git·python·pycharm