欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > git配置(1): 根据remote自动选择账号执行commit

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

2025/6/6 21:03:54 来源:https://blog.csdn.net/baiyu33/article/details/148293570  浏览:    关键词:git配置(1): 根据remote自动选择账号执行commit

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

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

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

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

[user]name = my_personal_nameemail = my_personal_email@foxmail.com    

发现 git 2.45 版本可以根据正则匹配到 remote 或 branch, 来载入特定的配置文件; 特定配置文件中使用公司账号即可:

https://git-scm.com/docs/git-config#_includes

[includeIf "hasconfig:remote.*.url:https://*.my_company.com/**"]path = .gitconfig-my_company

.gitconfig-my_company 内容:

[user]name = my_account_at_companyemail = my_account_at_company@my_company.com

ubuntu-22.04 默认 git 版本不识别上述语法, 需要加 ppa 源更新 git:

sudo add-apt-repository -y ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git -y

参考:

  • https://pliutau.com/using-different-git-emails/
  • https://git-scm.com/docs/git-config#_includes

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词