根据远程克隆URL为Git配置使用不同的user.email和user.name [英] Use a different user.email and user.name for Git config based upon remote clone URL

查看:116
本文介绍了根据远程克隆URL为Git配置使用不同的user.email和user.name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样配置全局~/.gitconfig属性user.nameuser.email:

I configure my global ~/.gitconfig properties user.name and user.email like this:

git config --global user.email "mkobit@example.com" 
git config --global user.name "mkobit"

这是我要用于个人项目,开源内容等的默认配置.

This is the default configuration I want for working on personal projects, open source stuff, etc.

当我在特定领域(例如公司领域)中处理项目时,我在克隆每个存储库时都会对其进行配置,以使其使用不同的user.name/user.email:

When I am working on a project from a specific domain, a corporate domain for example, I am configuring it for each repository when I clone it so that it uses a different user.name/user.email:

git clone ssh://git@git.mycorp.com:1234/groupA/projectA.git
cd projectA
git config user.email "mkobit@mycorp.com"
git config user.name "m.kobit"

一个不错的选择是设置一个别名来克隆这些类型的存储库:

One decent option would be to setup an alias for cloning these kinds of repositories:

git config --global alias.clonecorp 'clone \ 
        -c user.name="m.kobit" -c user.email="mkobit@mycorp.com"'
git clonecorp ssh://git@git.mycorp.com:1234/groupA/projectA.git

这两种方法都容易出错,因为它们都取决于我是否精明并遵循正确的步骤.证据表明,这几乎可以保证我在某个时候搞砸.

Both of these can be error prone because they both depend on me being smart and following the right steps. Evidence shows that this is near-guaranteed for me to screw-up sometime.

是否有一种配置Git的方式,使得来自某个域(本示例中的mycorp.com)的存储库将以某种方式配置?

Is there a way to configure Git such that repositories from a certain domain (the mycorp.com in this example) will be configured a certain way?

推荐答案

Git 2.13 引入了一项称为 conditional includes 的功能.在2.13中,唯一受支持的配置是文件系统路径.在这种情况下,这很容易使用,因为我已经将它们分开了.

The release of Git 2.13 has introduced a feature called conditional includes. In 2.13 the only supported configuration is filesystem path. That is easy to use in this case because I am already separating them.

发行说明中提供的示例为:

The example provided on the release notes is:

您可以在主目录的~/.gitconfig文件中配置两个条件包含:

You can configure two conditional includes in your home directory's ~/.gitconfig file:

[includeIf "gitdir:~/work/"]
    path = .gitconfig-work
[includeIf "gitdir:~/play/"]
    path = .gitconfig-play

现在,您可以将所需的任何选项放入这些文件中:

Now you can put whatever options you want into those files:

$ cat ~/.gitconfig-work
[user]
name = Serious Q. Programmer
email = serious.programmer@business.example.com

$ cat ~/.gitconfig-play
[user]
name = Random J. Hacker
email = rmsfan1979@example.com


旧答案

在全局配置 Git 2.8 中,添加了c7>,该内容要求用户在提交之前设置其user.emailuser.name.这是Github链接的博客文章中的相关文本:

In Git 2.8, a global configuration user.useconfigonly has been added that insists the user set their user.email and user.name are set before committing. Here is the relevant text from the linked blog post by Github:

但是,例如,如果您希望Git为您的开源项目使用一个电子邮件地址,为您的工作项目使用另一个电子邮件地址,那么您无疑犯了一个错误,即在没有先设置电子邮件的情况下提交到新的Git存储库该存储库中的地址.在这种情况下,Git会发出警告,但无论如何都会使用它从本地系统主机名中猜测的电子邮件地址来创建提交.如果您要为不同的项目尝试执行诸如不同地址之类的复杂操作,那么几乎可以肯定这不是您想要的.

But if, say, you want Git to use one email address for your open source projects and a different one for your work projects, you've undoubtedly made the mistake of committing to a new Git repository without having first set your email address in that repository. In this situation, Git emits a warning, but it creates the commit anyway, using an email address that it guesses from the local system hostname. If you're trying to do something as complicated as different addresses for different projects, this is almost certainly not what you want.

现在,您可以告诉Git不要猜测,而是要坚持在将其提交之前明确设置user.name和user.email:

Now you can tell Git not to guess, but rather to insist that you set user.name and user.email explicitly before it will let you commit:

git config --global user.useconfigonly true

这不能解决基于某些克隆URL的自动配置,但是通过在开始时强制进行配置,确实使该过程的出错几率降低了.

This doesn't solve the auto-config based on certain clone URLs, but does make the process a little bit less error prone by forcing configuration at the start.

这篇关于根据远程克隆URL为Git配置使用不同的user.email和user.name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆