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

参考:

相关推荐
再吃一根胡萝卜29 分钟前
Git 常见问题解决:误在分支 A 提交后,如何转移到新分支 B
git
锐策2 天前
Git checkout 与 Git reset 核心区别解析(分支与版本关联逻辑)
运维·git
.Shu.2 天前
git实战(7)git常用命令速查表
大数据·git
wniuniu_2 天前
git增加ignore文件
git
Jia-Hui Su2 天前
GDSFactory环境配置(PyCharm+Git+KLayout)
git·python·pycharm
陪我一起学编程2 天前
创建Vue项目的不同方式及项目规范化配置
前端·javascript·vue.js·git·elementui·axios·企业规范
你是我的幸运儿2 天前
git分支命名规范
git
果子⌂3 天前
Git+Jenkins实战(一)
运维·git·jenkins
苦逼IT运维3 天前
Jenkins + SonarQube 从原理到实战四:Jenkins 与 Gerrit 集成并实现自动任务
运维·git·测试工具·ci/cd·jenkins
_童年的回忆_3 天前
Linux下解决Git保存用户名和密码的方法
linux·运维·git