首页 > 解决方案 > 是否可以为不同的站点设置全局 git 配置?

问题描述

想象一下,你有这个 git 项目文件夹结构:

是否可以在一个地方的某个地方定义使用证书的 git auth 逻辑应该为相应路径之一下的所有项目配置?也许还有不同的用户签名?

标签: gitauthenticationconfiguration

解决方案


对于访问:您可以使用您的~/.ssh/config文件为不同的服务器设置不同的密钥。

# in your ~/.ssh/config file :

Host github.com
    IdentityFile path/to/github_key

Host gitlab.com
    IdentityFile path/to/gitlab_key

设置后,与github.com(resp. gitlab.com) 的任何 ssh 交互 - 例如 : git clone git@github.com:repo, git fetch, git push... - 默认情况下将使用github_key(resp. gitlab_key)。


对于git配置参数(例如:user.nameuser.email),您可以按照@choroba 的建议使用includeIf部分(链接到文档):

# in your .gitconfig file :
[includeIf "gitdir:~/github/**"]
    path = ~/path/to/github_conf_file

[includeIf "gitdir:~/gitlab/**"]
    path = ~/path/to/gitlab_conf_file

并在和中设置user.nameuser.email值。github_conf_filegitlab_conf_file


推荐阅读