将 git config 作为存储库的一部分存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18329621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:47:43  来源:igfitidea点击:

Storing git config as part of the repository

git

提问by Zaar Hai

I'm using filters to mangle files during checkout like described here. Now the problem is that filter definition is only stored in my local configuration file:

我在结帐期间使用过滤器来处理文件,如here所述。现在的问题是过滤器定义只存储在我的本地配置文件中:

$ cat .git/config
....
[filter "dater"]
        smudge = /home/.../expand_date
        clean = perl -pe \"s/\\\$Date[^\\\$]*\\\$/\\\$Date\\\$/\"

If my coworkers want to benefit from this Dateexpansion, they need to copy my filter definition. And if I change it, I need to notify them, etc..

如果我的同事想从这个Date扩展中受益,他们需要复制我的过滤器定义。如果我改变它,我需要通知他们,等等。

So can I store this filter definition part of .git/configin repository and make git use it?

那么我可以将这个过滤器定义部分存储.git/config在存储库中并让 git 使用它吗?

回答by Alexander Yancharuk

There are 3 supported scopes of .gitconfigfile: --system, --global, --local. You can also create a custom configuration file, and include it in one of the supported files.

有 3 个支持的.gitconfig文件范围:--system, --global, --local. 您还可以创建自定义配置文件,并将其包含在支持的文件之一中。

For your needs custom- is the right choice. Instead of writing your filter in .git/configyou should save it in .gitconfigfile in your repository root:

为您的需要定制——是正确的选择。.git/config您应该将过滤器保存.gitconfig在存储库根目录中的文件中,而不是在其中编写过滤器:

your-repo/
│
├── .git/
│   ├── config
│
├── .gitconfig
│

Create the .gitconfigwith your filter and commit the changes. Then your colleagues will always keep it updated -- but they will have to include it manually. It is not possible to automatically include your custom configuration filethrough git alone, because it creates a security vulnerability.

.gitconfig使用您的过滤器创建并提交更改。然后您的同事将始终保持更新——但他们必须手动包含它。单独通过 git自动包含您的自定义配置文件是不可能的,因为它会产生安全漏洞。

To apply this configuration for a single repository, each user will need to run the following command in your-repo/:

要将此配置应用于单个存储库,每个用户都需要在 中运行以下命令your-repo/

git config --local include.path ../.gitconfig

Be careful not to store personal data in the custom .gitconfig, like user.*, keep those in your global .gitconfig.

注意不要将个人数据存储在自定义中.gitconfig,例如user.*,将它们保存在您的全局中.gitconfig

回答by tenhobi

You can not use .gitconfigfile in a git repository by default, but you can link to it so the git config will be versioned.

.gitconfig默认情况下,您不能在 git 存储库中使用文件,但您可以链接到它,以便对 git 配置进行版本控制。

You can link to it like that:

你可以像这样链接到它:

[include]
  path = ../.gitconfig

I have created a simple scriptgitconfig.shwhich do it for you (much faster than copy) + simple .gitconfigfile so if you want, take a look to this repo https://github.com/HoBi/dotfiles.

我已经创建了一个简单的脚本gitconfig.sh来为你做这件事(比复制快得多)+简单的.gitconfig文件,所以如果你愿意,看看这个 repo https://github.com/HoBi/dotfiles



EDIT: I have deleted the file, but you can find it here https://github.com/tenhobi/dotfiles/blob/7e4376c006c508370b82bc7bd37173fab51dbd01/git/.gitconfig.sh

编辑:我已删除该文件,但您可以在这里找到它https://github.com/tenhobi/dotfiles/blob/7e4376c006c508370b82bc7bd37173fab51dbd01/git/.gitconfig.sh