当前位置: 首页 > news >正文

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

相关文章:

  • 【掌握文件操作】(下):文件的顺序读写、文件的随机读写、文件读取结束的判定、文件缓冲区
  • C++异常处理机制
  • :inline=“true“会发生什么
  • 酒店用品源头厂家推荐
  • SQL中的锁机制
  • mybatis的mapper对应的xml写法
  • 如何解决网站服务器的异常问题?
  • Gin项目脚手架与标配组件
  • 网站服务器出现异常的原因是什么?
  • 回头看,FPGA+RK3576方案的功耗性能优势
  • 网站缓存入门与实战:浏览器与Nginx/Apache服务器端缓存,让网站速度起飞!(2025)
  • Nginx代理、缓存与Rewrite
  • 智能驾驶感知算法任务简介
  • 2025年渗透测试面试题总结-匿名[校招]安全工程师(甲方)(题目+回答)
  • Vim 中设置插入模式下输入中文
  • 【烧脑算法】定长滑动窗口:算法题中的“窗口”智慧
  • 大模型在先天性肌性斜颈诊疗全流程中的应用研究报告
  • 【25-cv-00656】Whitewood律所代理Olga Drozdova 蝴蝶版权图维权案
  • 深入详解(0020,0052) Frame of Reference UID在序列空间定位中的定义与作用
  • 系统赛数据库的一些记录
  • 专做茶叶的网站/免费seo优化
  • 网站建设公司哪家强/山东网络推广优化排名
  • 怎么进行网站备案/百度下载app下载安装到手机
  • 万网博通官网/seo最好的工具
  • 石家庄网站制作福州/html网页制作软件有哪些
  • 泸州网站建设多少钱/如何优化标题关键词