git 是否可以在 .gitconfig 中包含文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1557183/
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 07:12:22  来源:igfitidea点击:

Is it possible to include a file in your .gitconfig

gitgithub

提问by David Reynolds

I'd like to include a file in my .gitconfig that has my github settings - is this possible?

我想在我的 .gitconfig 中包含一个包含我的 github 设置的文件 - 这可能吗?

Can I do something like this:

我可以做这样的事情:

[core]
    include = /path/to/file

回答by Mike Morearty

Git (1.7.10+) now supportsthis syntax in .gitconfig:

Git (1.7.10+) 现在支持以下语法.gitconfig

[include]
    path = /path/to/file

See herefor a detailed description of the git change and its edge cases.

有关git 更改及其边缘情况的详细说明,请参见此处

By the way, a couple of subtleties worth pointing out:

顺便说一下,有几个微妙之处值得指出:

  1. Environment-variable expansion, e.g. $HOME, is not supported. (Expansion of ~appeared in Git 1.7.10.2.)

  2. If a relative path is specified, then it is relative to the .gitconfig file that has the [include]statement. This works correctly even across chained includes -- e.g. ~/.gitconfigcan have:

    [include]
        path = subdir/gitconfig
    

    and subdir/gitconfigcan have:

    [include]
        path = nested_subdir/gitconfig
    

    ... which will cause subdir/nested_subdir/gitconfigto be loaded.

  3. If git can't find the target file, it silently ignores the error. This appears to be by design.

  1. $HOME不支持环境变量扩展,例如。(扩展~出现在 Git 1.7.10.2 中。)

  2. 如果指定了相对路径,则它相对于包含该[include]语句的 .gitconfig 文件。即使跨链式包含~/.gitconfig也能正常工作——例如可以有:

    [include]
        path = subdir/gitconfig
    

    并且subdir/gitconfig可以有:

    [include]
        path = nested_subdir/gitconfig
    

    ...这将导致subdir/nested_subdir/gitconfig加载。

  3. 如果 git 找不到目标文件,它会默默地忽略错误。这似乎是设计使然。

回答by VonC

Update 2012:

2012 年更新:

See Mike Morearty's answer:

参见Mike Morearty回答

Includes

包括

You can include one config file from another by setting the special include.pathvariable to the name of the file to be included.
The included file is expanded immediately, as if its contents had been found at the location of the include directive.
If the value of the include.pathvariable is a relative path, the path is considered to be relative to the configuration file in which the include directive was found.
The value of include.pathis subject to tilde expansion: ~/is expanded to the value of $HOME, and ~user/to the specified user's home directory.

您可以通过将特殊include.path变量设置为要包含的文件的名称来包含另一个配置文件。
被包含的文件会立即展开,就好像在 include 指令的位置找到了它的内容一样。
如果include.path变量的值是相对路径,则该路径被认为是相对于包含指令所在的配置文件的。
的值include.path受波浪号扩展的影响:~/扩展为 的值$HOME,并扩展到~user/指定用户的主目录。



I do not think so.

我不这么认为。

I would rather put that setting in the ~/.gitconfigfile

我宁愿将该设置放在~/.gitconfig文件中

User-specific configuration file. Also called "global" configuration file.

用户特定的配置文件。也称为“全局”配置文件。

That way, it completes the .gitconfig project-specific file, without being published when pushed to GitHub. See also this SO answerfor more on the global config file.
Git has 3 config files.

这样,它完成了 .gitconfig 项目特定的文件,而不会在推送到 GitHub 时发布。有关全局配置文件的更多信息,另请参阅此SO 答案
Git 有3 个配置文件



bjeanesadds in the comments:

bjeanes在评论中补充道:

it looks like everyone missed the point of this question.
David obviously wants to push up a repo of all his dot files (bashrc, gemrc, etc.) INCLUDING his .gitconfigso he can have all his settings on all his machines.
A way to push parts of a .gitconfigfile by including and ignoring private entries is what he (and I, for that matter) is after.

看起来每个人都错过了这个问题的重点。
大卫显然想推他的所有点文件的回购(bashrcgemrc包括他等),.gitconfig所以他可以有他所有的对他的所有机器设置。通过包含和忽略私有条目
来推送.gitconfig文件的一部分的方法是他(和我,就此而言)所追求的。

A possible way would be to use a smudge/clean filter driverto decrypt/encrypt one file with private sensitive informations (see this thread), in order to complete a local file like ~/.gitconfig with the decrypted parts that are relevant to that file.

一种可能的方法是使用污迹/清洁过滤器驱动程序来解密/加密具有私人敏感信息的文件(请参阅此线程),以便完成像 ~/.gitconfig 这样的本地文件,其中包含与该文件相关的解密部分文件。

That way you can have a Git repo with all your dot files, plus one file with encrypted information meant to be decrypted and added to said dot files.

这样你就可以拥有一个包含所有点文件的 Git 存储库,以及一个包含加密信息的文件,该文件旨在解密并添加到所述点文件中。

alt text

替代文字

In .gitattributes(or .git/info/a..) use:

.gitattributes(或.git/info/a..)使用:

myPrivateInfosFile filter=gpg diff=gpg

In your repo .config file:

在你的 repo .config 文件中:

[filter "gpg"]
smudge = gpg -d -q --batch --no-tty
clean = gpg -ea -q --batch --no-tty -r C920A124
[diff "gpg"]
textconv = decrypt

(a GPG-based solutionmeans, off course, you have communicated your private/public keys by another mean onto the destination computer where you want to restore all your dot files by cloning this special repo)

(当然,基于 GPG 的解决方案意味着,您已通过另一种方式将您的私钥/公钥传送到要通过克隆此特殊存储库来恢复所有 dot 文件的目标计算机上)

Actually, in your case, the smudge script needs to be completed as it must, after decrypted that file, go on and add relevant parts to your global ~/.gitconfigfile (unless you overwrite the global config file with another location) or other dot files for that matter.

实际上,在您的情况下,需要完成涂抹脚本,因为它必须在解密该文件后,继续将相关部分添加到您的全局~/.gitconfig文件(除非您用另一个位置覆盖全局配置文件)或其他点文件事情。

https://kerneltrap.org/mailarchive/git/2008/3/13/1153274/thread(gpg inconveniences are discussed further in this thread) (this is different than having a full encrytped Git repo, as discussed here)

https://kerneltrap.org/mailarchive/git/2008/3/13/1153274/thread(gpg 的不便将在此线程中进一步讨论)(这与拥有完整加密的 Git 存储库不同,如下所述

回答by kenorb

You may load it from the command-line:

您可以从命令行加载它:

$ git config --local include.path "/path/to/.gitconfig"

Use "$PWD"/.gitconfiginstead, if you want to load the file from the current directory.

"$PWD"/.gitconfig如果要从当前目录加载文件,请改用。

After running above command, the following lines are added into your .git/configfile:

运行上述命令后,将以下行添加到您的.git/config文件中:

[include]
        path = /path/to/.gitconfig

回答by Geoff Lanotte

I believe you can accomplish this using defunkt's hub tool. This is a wrapper for the git command which among other things, allows you to have GITHUB_USERand GITHUB_TOKENenvironment variables. Which will override the settings in the local .gitconfigfile.

我相信您可以使用defunkt 的集线器工具来完成此操作 。这是 git 命令的包装器,除其他外,它允许您拥有GITHUB_USERGITHUB_TOKEN环境变量。这将覆盖本地.gitconfig文件中的设置。

Then to make it seamless the user you pointed to aliased alias git=hubin his ZSH config. You should be able to then source a local file where you set your environment variables and push your repository to the public world with all of your private information in tact.

然后使您alias git=hub在他的ZSH 配置中指向别名的用户无缝连接。然后,您应该能够获取一个本地文件,您可以在其中设置环境变量,并将您的存储库推送到公共世界,并包含您所有的私人信息。

**NOTE for homebrew users on OSX, you can install the tool via brew install hub.

**注意 OSX 上的自制软件用户,您可以通过brew install hub.