调整你的 Git 配置以适应多个用户 ID 的需要
Git

> 
> 可以使用相同的机器用于工作和个人的 Git 提交，而无需手动重置你的配置。
> 
> 
> 


![](/data/attachment/album/202011/12/013805t4u4nu57rc6ur7nt.jpg)


Git 的 [git config](https://git-scm.com/docs/git-config) 命令可以让你为 Git 设置仓库或全局选项。它有很多选项，其中的一个选项 `includeIf` 在你使用在 Git 时有双重角色时非常方便，比如说，你既是全职的开发者，又在业余时间为开源项目做贡献。在这种情况下，大多数人都不想为两个角色使用一个共同的配置，或者，至少，他们肯定希望保持配置的某些部分是不同的，尤其是当他们在两个角色中使用同一台计算机时。


我的情况就是这样，所以我在 Git 配置中保留了两组不同的邮件 ID。这样一来，在我工作场所的项目仓库中提交的内容就会使用我办公室的邮件 ID，而在我个人 GitHub 帐户中提交的内容则使用我个人的邮件 ID。


以下是我的全局配置（维护在 `$HOME/.gitconfig`）中的一个片段，我将在下文中介绍。



```
[includeIf "gitdir:~/priv_scm/"]
        path = ~/priv_scm/.gitconfig
[includeIf "gitdir:~/work_scm/"]
        path = ~/work_scm/.gitconfig

```

### 什么是 includeIf？


`includeIf.condition.path` 变量，是 `include` 配置指令的一部分，允许你有条件地设置自定义配置。同时，自定义配置的路径也可以作为指令的一部分来设置。


这个指令支持三个关键字：`gitdir`、`gitdir/I` 和 `onbranch`。我将简单解释一下 `gitdir`，我在上面的代码片段中使用了它。你可以在[文档](https://git-scm.com/docs/git-config#_conditional_includes)中了解其他两个关键词。


在 `includeIf` 指令中使用 `gitdir` 关键字会对模式进行条件检查。根据规则，如果当前工作目录与 `gitdir` 中指定的目录模式相匹配，那么它就会从给定的路径中选取配置。我将在配置片段上应用这个规则来展示它是如何被应用的。


在配置片段中，你可以看到一个简单的模式，`~/`，它与 `gitdir` 关键字一起使用。这个模式会被存储在 `$HOME` 环境变量中的值所替代。


### 如何使用它


如果你使用同一个系统在 GitHub 或 GitLab 上的开源项目上工作，并在工作中提交到 Git 仓库，你可以有两个顶级目录，比如 `$HOME/priv_scm` 和 `$HOME/work_scm`。在这两个目录中，你可以有两个单独的 `.gitconfig` 文件，其中包含与你的 `user.name` 和 `user.email` 相关的设置。然而，它们也可以包含存储在 `$HOME` 的全局 `.gitconfig` 中，它可以保存两个环境通用的所有自定义项。


这里是一个例子 `$HOME/priv_scm/.gitconfig` 的片段：



```
$ cat $HOME/priv_scm/.gitconfig
[user]
    name  = Ramanathan Muthiah
    email = &lt;personal-mailid-goes-here&gt;

```

有了这个配置，你就可以切换目录，并开始在开源项目上工作，而无需手动重置一些与 Git 相关的配置。这些更改会在主 `.gitconfig` 中借助 `includeIf` 条件指令自动处理。


### Git 小贴士


希望这个小贴士能帮助你组织 Git 项目。你最喜欢的 Git 小贴士是什么？请在评论中分享吧！




---


via: <https://opensource.com/article/20/10/git-config>


作者：[Ramanathan M](https://opensource.com/users/muthiahramanathan) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Git's [git config](https://git-scm.com/docs/git-config) command (hereafter referred to as "config") enables you to set repository or global options for Git. It has many options, and one of them, `includeIf`

, is handy when you have dual roles using Git, for example, working full time as a developer and contributing to open source projects in your spare time. Most people in this situation don't want to use a common config for both roles or, at the very least, would rather keep certain portions of their config distinct, especially if they use the same computer in both roles.

This is my situation, so I keep two separate sets of mail IDs as part of my Git config. This way, commits in repositories related to projects at my workplace carry my office mail ID, whereas the commits made to repositories hosted in my personal GitHub account use my personal mail ID.

To illustrate, here is a sample snippet from my global config (maintained at `$HOME/.gitconfig`

), which I will describe below.

```
[includeIf "gitdir:~/priv_scm/"]
path = ~/priv_scm/.gitconfig
[includeIf "gitdir:~/work_scm/"]
path = ~/work_scm/.gitconfig
```


## What's includeIf?

The `includeIf.condition.path`

variable, part of the `include`

config directives, allows you to set a custom config conditionally. Also, the path to the custom config can be set as part of the directive.

This directive supports three keywords: `gitdir`

, `gitdir/I`

, and `onbranch`

. I will briefly explain `gitdir`

, which I use in the code snippet above; you can learn about the other two in the [docs](https://git-scm.com/docs/git-config#_conditional_includes).


Using the `gitdir`

keyword in the `includeIf`

directive causes a conditional check to be performed on the pattern. Per the rule, if the current working directory matches the specified directory pattern in `gitdir`

, then it picks the config from the given path. I'll apply this rule on the config snippet to show how it's evaluated.

In the config snippet, you can see a simple pattern, `~/`

, used along with the `gitdir`

keyword. This pattern is substituted with the value stored in the `$HOME`

environment variable.

## How to use it

If you're using the same system to work on open source projects hosted on GitHub or GitLab and committing to Git repositories at work, you can have two top-level directories, like `$HOME/priv_scm`

and `$HOME/work_scm`

. Within these two directories, you can have two separate `.gitconfig`

files with settings related to your `user.name`

and `user.email`

. However, they can also be included in a global `.gitconfig`

stored at `$HOME`

that can hold all the customizations common to both environments.

Here is a snippet of an example `$HOME/priv_scm/.gitconfig`

:

```
$ cat $HOME/priv_scm/.gitconfig
[user]
name = Ramanathan Muthiah
email = <personal-mailid-goes-here>
```


With this config in place, you can switch directories and start working on open source projects without resetting some of the config related to Git manually. These changes are handled automatically in the main `.gitconfig`

with the aid of the `includeIf`

conditional directive.

## Git tips

I hope this tip helps you organize your Git projects. What are your favorite Git tips? Share them in the comments!

## Comments are closed.