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

[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:

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

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值