我的 Git 配置中的设置来自哪里?

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

Where do the settings in my Git configuration come from?

gitmsysgitgit-config

提问by RyanW

I've noticed that I have two listings for core.autocrlfwhen I run git config -l

我注意到core.autocrlf我运行时有两个列表git config -l

$ git config -l
core.symlinks=false
core.autocrlf=false
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.packsizelimit=2g
help.format=html
http.sslcainfo=/bin/curl-ca-bundle.crt
sendemail.smtpserver=/bin/msmtp.exe
diff.astextplain.textconv=astextplain
rebase.autosquash=true
user.name=name
[email protected]
core.autocrlf=true

Those last three (from user.name down) are the only ones in my C:\users\username\.gitconfigfile. Where are all of the other ones coming from? Why is core.autocrlf listed twice?

最后三个(从 user.name 开始)是我C:\users\username\.gitconfig文件中唯一的。其他的都是从哪里来的?为什么 core.autocrlf 被列出两次?

This is with MSysGit 1.8.3, and I also have Sourcetree installed (Windows 7). In Sourcetree I have unchecked the "Allow Sourcetree to modify your global Git config files"

这是 MSysGit 1.8.3,我还安装了 Sourcetree(Windows 7)。在 Sourcetree 中,我取消选中“允许 Sourcetree 修改您的全局 Git 配置文件”

回答by

Git checks four places for a configuration file:

Git 检查配置文件的四个位置:

  1. Your machine's system .gitconfigfile.
  2. Your user .gitconfigfile located at ~/.gitconfig.
  3. A second user-specific configuration file located at $XDG_CONFIG_HOME/git/configor $HOME/.config/git/config.
  4. The local repository's configuration file .git/config.
  1. 您机器的系统.gitconfig文件。
  2. 您的用户.gitconfig文件位于~/.gitconfig.
  3. 位于$XDG_CONFIG_HOME/git/config或 的第二个特定于用户的配置文件$HOME/.config/git/config
  4. 本地存储库的配置文件.git/config.

The settings cascade in the following order, with each file adding or overriding settings defined in the file above it.

设置按以下顺序级联,每个文件添加或覆盖其上方文件中定义的设置。

  1. System configuration.
  2. User configuration.
  3. Repository-specific configuration.
  1. 系统配置。
  2. 用户配置。
  3. 特定于存储库的配置。

You can see what each file has defined using the following commands:

您可以使用以下命令查看每个文件的定义:

# System, applies to entire machine and all users
$ git config --system --list
$ git config --system --edit

# User defined
$ git config --global --list
$ git config --global --edit

You can see what just the repository-specific file has defined by opening up the file .git/configfor that repository.

您可以通过打开该存储库的文件来查看特定于存储库的文件所定义的内容.git/config

If you're using MSysGit on Windows, you'll probably find your user ~/.gitconfigfile where ever %homepath%points to if you use echo %homepath%from a Windows command prompt.

如果您在 Windows 上使用 MSysGit,如果您从 Windows 命令提示符使用,您可能会~/.gitconfig在曾经%homepath%指向的位置找到您的用户文件echo %homepath%

From the documentation for git config:

文档中git config

If not set explicitly with --file, there are four files where git configwill search for configuration options:

  • $(prefix)/etc/gitconfig

    System-wide configuration file.

  • $XDG_CONFIG_HOME/git/config

Second user-specific configuration file. If $XDG_CONFIG_HOMEis not set or empty, $HOME/.config/git/configwill be used. Any single-valued variable set in this file will be overwritten by whatever is in ~/.gitconfig. It is a good idea not to create this file if you sometimes use older versions of Git, as support for this file was added fairly recently.

  • ~/.gitconfig

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

  • $GIT_DIR/config

    Repository specific configuration file.

If no further options are given, all reading options will read all of these files that are available. If the global or the system-wide configuration file are not available they will be ignored. If the repository configuration file is not available or readable, git configwill exit with a non-zero error code. However, in neither case will an error message be issued.

The files are read in the order given above, with last value found taking precedence over values read earlier. When multiple values are taken then all values of a key from all files will be used.

All writing options will per default write to the repository specific configuration file. Note that this also affects options like --replace-alland --unset. git configwill only ever change one file at a time.

You can override these rules either by command-line options or by environment variables. The --globaland the --systemoptions will limit the file used to the global or system-wide file respectively. The GIT_CONFIGenvironment variable has a similar effect, but you can specify any filename you want.

如果没有显式设置--file,则有四个文件git config将在其中搜索配置选项:

  • $(prefix)/etc/gitconfig

    系统范围的配置文件。

  • $XDG_CONFIG_HOME/git/config

第二个特定于用户的配置文件。如果$XDG_CONFIG_HOME未设置或为空,$HOME/.config/git/config将使用。此文件中设置的任何单值变量都将被~/.gitconfig. 如果您有时使用旧版本的 Git,最好不要创建此文件,因为最近才添加了对该文件的支持。

  • ~/.gitconfig

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

  • $GIT_DIR/config

    存储库特定的配置文件。

如果没有给出更多选项,所有读取选项将读取所有这些可用文件。如果全局或系统范围的配置文件不可用,它们将被忽略。如果存储库配置文件不可用或不可读,git config将以非零错误代码退出。但是,在任何情况下都不会发出错误消息。

文件按上面给出的顺序读取,发现的最后一个值优先于之前读取的值。当取多个值时,将使用来自所有文件的键的所有值。

默认情况下,所有写入选项都将写入存储库特定的配置文件。请注意,这也会影响--replace-all和 等选项 --unsetgit config一次只会更改一个文件。

您可以通过命令行选项或环境变量覆盖这些规则。在--global--system选项将限制分别用于全球或全系统文件的文件。该GIT_CONFIG环境变量有类似的效果,但您可以指定任何你想要的文件名。

回答by VonC

You don't have to guess anymore which config has been set to where, with git 2.8! (March 2016)

使用 git 2.8,您不必再猜测哪个配置已设置在何处!(2016 年 3 月)

See commit 70bd879, commit 473166b, commit 7454ee3, commit 7454ee3(19 Feb 2016), commit 473166b, commit 7454ee3(19 Feb 2016), commit 7454ee3(19 Feb 2016), and commit a0578e0(17 Feb 2016) by Lars Schneider (larsxschneider).
(Merged by Junio C Hamano -- gitster--in commit dd0f567, 26 Feb 2016)

提交70bd879提交473166b提交7454ee3提交7454ee3(2016年2月19日),提交473166b提交7454ee3(2016年2月19日),提交7454ee3(2016年2月19日),以及提交a0578e0(2016年2月17日)由拉斯施耐德(larsxschneider.
(由Junio C gitsterHamano合并-- --dd0f567 提交中,2016 年 2 月 26 日)

config: add '--show-origin' option to print the origin of a config value

If config values are queried using 'git config' (e.g. via --get, --get-all, --get-regexp, or --listflag) then it is sometimes hard to find the configuration file where the values were defined.

Teach 'git config' the '--show-origin' option to print the source configuration file for every printed value.

配置:添加“ --show-origin”选项以打印配置值的来源

如果使用 ' git config'(例如 via --get--get-all--get-regexp--listflag)查询配置值,那么有时很难找到定义这些值的配置文件。

教 ' git config' ' --show-origin' 选项为每个打印值打印源配置文件。

The git configman pagewill now indicates:

git config手册页现在会显示:

--show-origin:

Augment the output of all queried config options with the origin type (file, standard input, blob, command line) and the actual origin (config file path, ref, or blob id if applicable).

使用源类型(文件、标准输入、blob、命令行)和实际源(配置文件路径、ref 或 blob id,如果适用)增加所有查询配置选项的输出。

For example:

例如:

git config --list --show-origin

That will return:

那将返回:

    file:$HOME/.gitconfig   user.global=true
    file:$HOME/.gitconfig   user.override=global
    file:$HOME/.gitconfig   include.path=$INCLUDE_DIR/absolute.include
    file:$INCLUDE_DIR/absolute.include  user.absolute=include
    file:.git/config    user.local=true
    file:.git/config    user.override=local
    file:.git/config    include.path=../include/relative.include
    file:.git/../include/relative.include   user.relative=include
    command line:   user.cmdline=true

For one setting, as commentedby wisbucky:

对于一种设置,正如wisbucky评论那样

git config --show-origin --get-all core.autocrlf

    file:"D:\prgs\git\latest\mingw64/etc/gitconfig"     true
    file:C:/Users/vonc/.gitconfig   false


With Git 2.26 (Q1 2020), you can add the --show-scopeoption:

使用 Git 2.26(2020 年第一季度),您可以添加--show-scope选项

git config -l --show-origin --show-scope

回答by jinxcat2008

After having previously installed Git for Windowsand subsequently uninstalling it, I found that there is a configuration file installed at C:\Users\All Users\Git\configwhich is a system level configuration file which persists and will affect any future MinGW32 Git packages (in my case, I was running a portable MinGW32 Git package provided by my company). When I ran

在之前为 Windows安装了Git并随后将其卸载后,我发现安装了一个配置文件,C:\Users\All Users\Git\config该文件是一个系统级配置文件,该文件持续存在并将影响任何未来的 MinGW32 Git 包(在我的情况下,我运行的是便携式 MinGW32我公司提供的Git包)。当我跑

git config --system --edit

it would show me the system configuration file located at mingw32/etc/gitconfig, but it would still load the values from the first location as well. This showed up as a warning that configuration values clashed when trying to use Git LFS.

它会显示位于 的系统配置文件mingw32/etc/gitconfig,但它仍会从第一个位置加载值。这显示为尝试使用Git LFS时配置值发生冲突的警告。

WARNING: These git config values clash:
  git config "http.sslcainfo" = "C:/Users/foo/AppData/Local/Programs/Git/mingw64/ssl/certs/ca-bundle.crt"
  git config "http.sslcainfo" = "/ssl/certs/ca-bundle.crt"

(Note: this may also be a situation where LFS warnings are being too assertive, #861)

(注意:这也可能是 LFS 警告过于自信的情况,#861

回答by CBIII

git config -lshows all inherited values from system, global, and local.

git config -l显示所有从系统、全局和本地继承的值。

So you have another configuration file somewhere that is being loaded along with your user-defined .gitconfigfile.

因此,您在某处有另一个配置文件与您的用户定义.gitconfig文件一起加载。

回答by Ini

You can use --show-originin order to find out where the configurations come from.

您可以使用--show-origin以找出配置的来源。

Configuration files priority in Git for Windows:

Windows 版 Git 中的配置文件优先级:

...

$PROGRAMDATA/Git/config::
(Windows-only) System-wide configuration file shared with other Git implementations. Typically $PROGRAMDATApoints to C:\ProgramData.

$(prefix)/etc/gitconfig::
System-wide configuration file. (Windows-only) This file contains only the settings which are specific for this installation of Git for Windows and which should not be shared with other Git implementations like JGit, libgit2. --systemwill select this file.

$XDG_CONFIG_HOME/git/config::
Second user-specific configuration file. If $XDG_CONFIG_HOMEis not set or empty, $HOME/.config/git/configwill be used. Any single-valued variable set in this file will be overwritten by whatever is in ~/.gitconfig. It is a good idea not to create this file if you sometimes use older versions of Git, as support for this file was added fairly recently.

~/.gitconfig::
User-specific configuration file. Also called "global" configuration file.

$GIT_DIR/config::
Repository specific configuration file.

...

The files are read in the order given above, with last value found taking precedence over values read earlier.

...

...

$PROGRAMDATA/Git/config:
:(仅限 Windows)与其他 Git 实现共享的系统范围的配置文件。通常$PROGRAMDATA指向C:\ProgramData.

$(prefix)/etc/gitconfig::
系统范围的配置文件。(仅限 Windows)此文件仅包含特定于此 Windows 版 Git 安装的设置,不应与其他 Git 实现(如 JGit、libgit2)共享。 --system将选择此文件。

$XDG_CONFIG_HOME/git/config::
第二个特定于用户的配置文件。如果$XDG_CONFIG_HOME未设置或为空, $HOME/.config/git/config将使用。此文件中设置的任何单值变量都将被 ~/.gitconfig. 如果您有时使用旧版本的 Git,最好不要创建此文件,因为最近才添加了对该文件的支持。

~/.gitconfig::
用户特定的配置文件。也称为“全局”配置文件。

$GIT_DIR/config::
存储库特定的配置文件。

...

文件按上面给出的顺序读取,发现的最后一个值优先于之前读取的值。

...

Source: https://github.com/git-for-windows/git/blob/master@%7B2018-01-07%7D/Documentation/git-config.txt#L231

来源:https: //github.com/git-for-windows/git/blob/master@%7B2018-01-07%7D/Documentation/git-config.txt#L231

$PROGRAMDATAis an environment variable. You can get the value of those variables like this:

$PROGRAMDATA是环境变量。您可以像这样获取这些变量的值:

In Git Bash you need to use echo "$ProgramData". In CMD, you need to use echo %PROGRAMDATA%. Note that Git Bash apparently pretends that environment variables are case sensitive.

在 Git Bash 中,您需要使用echo "$ProgramData". 在 CMD 中,您需要使用echo %PROGRAMDATA%. 请注意,Git Bash 显然假装环境变量区分大小写。

What is $(prefix)?

什么是$(prefix)

The prefix is the top-level directory into which things get installed. In Git for Windows, that's either <some-path>/mingw64or <some-path>/mingw32.

前缀是安装东西的顶级目录。在Git中为Windows,这是无论是<some-path>/mingw64<some-path>/mingw32

回答by Pawel Cioch

On Windows 7 (maybe the same or similar for Windows 10), for Visual Studio and the Git command line, your global configuration is in:

在 Windows 7(可能与 Windows 10 相同或相似)上,对于 Visual Studio 和 Git 命令行,您的全局配置位于:

%USERPROFILE%\.gitconfig

(dot is in front of the file name)

(点在文件名前面)

But this is not honored by Sourcetree, at least in Git Embedded mode, and the configuration is in:

但是Sourcetree并不尊重这个,至少在Git Embedded模式下是这样,配置在:

%USERPROFILE%\AppData\Local\Atlassian\SourceTree\git_local\mingw32\etc\gitconfig

(no dot in front of the file name)

(文件名前没有点)

(I needed to update both files to modify my global Git settings for Git command and Sourcetree.)

(我需要更新这两个文件以修改 Git 命令和 Sourcetree 的全局 Git 设置。)

Another fun part. The Git hooks configuration was working from the AppData\Local\...location, but after more research via Process Monitor, I noticed somehow Sourcetree is also loading global from company mapped drive for my user.

另一个有趣的部分。Git hooks 配置在该AppData\Local\...位置工作,但经过Process Monitor 的更多研究后,我注意到 Sourcetree 也以某种方式从公司映射驱动器为我的用户加载全局。

This makes very little sense as very few applications lookup this location, but somehow Sourcetree does, so if you can't make it to work per location settings at Sourcetree, run Process Monitor and create a rule to log only path containing gitconfig, and you can find where really your global configuration is in case of a network-mapped user directory.

这没什么意义,因为很少有应用程序会查找此位置,但 Sourcetree 以某种方式这样做,因此如果您无法使其在 Sourcetree 的每个位置设置中工作,请运行 Process Monitor 并创建规则以仅记录包含 gitconfig 的路径,然后您在网络映射用户目录的情况下,可以找到您的全局配置的真正位置。

And this may not be even fault of Sourcetree, as I see now as I write this that git.exe is loading that, but this happens only for git.exe executed by Sourcetree, while a direct command line Git uses %USERPROFILE%\.gitconfig

这甚至可能不是 Sourcetree 的错,正如我现在看到的,当我写这篇文章时,git.exe 正在加载它,但这仅发生在 Sourcetree 执行的 git.exe 上,而 Git 使用的直接命令行 %USERPROFILE%\.gitconfig

Enter image description here

在此处输入图片说明

Finally I took all the results from Process Monitor, fed it into SQL Server and ran a query to get distinct results (no particular execution order just sorted by path):

最后,我从 Process Monitor 获取所有结果,将其输入 SQL Server 并运行查询以获得不同的结果(没有特定的执行顺序只是按路径排序):

Enter image description here

在此处输入图片说明

I don't know how those configurations relate to each-other, but I know some override another some settings works from one location some from another.

我不知道这些配置是如何相互关联的,但我知道有些会覆盖另一个,有些设置在一个位置工作,有些在另一个位置工作。

And the above list is invoked by Sourcetree, again direct a command line with Git seems to work fine with %USERPROFILE%\.gitconfig, and that is not on this list, but it would look like this (on Windows 7 ) C:\Users\pawel.cioch\.gitconfig

并且上面的列表由 Sourcetree 调用,再次使用 Git 直接命令行似乎可以正常工作%USERPROFILE%\.gitconfig,并且不在此列表中,但它看起来像这样(在 Windows 7 上)C:\Users\pawel.cioch\.gitconfig

回答by Tom

A complete answer for Windows (i.e. a Windows version of the accepted answer):

Windows 的完整答案(即已接受答案的 Windows 版本):

Like Linux, Windows has four levels of config files/settings and three are direct equivalents. The important thing to note is the other one - the 'All Apps/Users' one - particularly since this is where the installer sets values, e.g. 'core.autocrlf = true', and yet it can't be accessed from the command line so it causes confusion.

与 Linux 一样,Windows 有四个级别的配置文件/设置,其中三个是直接等效的。需要注意的重要事项是另一个 - “所有应用程序/用户” - 特别是因为这是安装程序设置值的地方,例如“core.autocrlf = true”,但无法从命令行访问所以它会引起混乱。

All Applications and Users

所有应用程序和用户

This is like a shared version of the ‘system' settings in case you have multiple Git applications installed. There is no 'git config' command to access these, but they still impact the net result for a setting.

如果您安装了多个 Git 应用程序,这就像“系统”设置的共享版本。没有 'git config' 命令来访问这些,但它们仍然影响设置的最终结果。

Configuration file location:

配置文件位置:

C:\ProgramData\Git\config

C:\ProgramData\Git\config

(Note that 'ProgramData' was 'All Users' on older versions of Windows.)

(请注意,在旧版本的 Windows 上,“ProgramData”是“所有用户”。)

System

系统

Configuration file location: C:/Program Files/Git/mingw64/etc/gitconfig

配置文件位置:C:/Program Files/Git/mingw64/etc/gitconfig

$ git config --system --list

User

用户

Configuration file location: %USERPROFILE%.gitconfig (This resolves to 'C:/Users/<username>')

配置文件位置:%USERPROFILE%.gitconfig(这解析为'C:/Users/<username>')

$ git config --global --list

Repository

存储库

Configuration file location: [current repository directory]/.git/config

配置文件位置:【当前仓库目录】/.git/config

$ git config --local --list

回答by VonC

In addition of git config -l --show-origin, that I presented here, with git 2.28 (March 2016), you now have, with Git 2.26 (Q1 2020)

此外git config -l --show-origin我在此处介绍的git 2.28(2016 年 3 月),您现在拥有 Git 2.26(2020 年第一季度)

git config -l --show-scope

# you can combine both options:
git config -l --show-origin --show-scope

git configlearned to show in which "scope", in addition to in which file, each config setting comes from.

git config学会了显示在哪个“ scope”中,除了在哪个文件中,每个配置设置来自。

See commit 145d59f, commit 9a83d08, commit e37efa4, commit 5c105a8, commit 6766e41, commit 6dc905d, commit a5cb420(10 Feb 2020), and commit 417be08, commit 3de7ee3, commit 329e6ec(24 Jan 2020) by Matthew Rogers (ROGERSM94).
(Merged by Junio C Hamano -- gitster--in commit 5d55554, 17 Feb 2020)

提交145d59f提交9a83d08提交e37efa4提交5c105a8提交6766e41提交6dc905d提交a5cb420(2020年2月10日),并提交417be08提交3de7ee3提交329e6ec(2020年1月24日),由马修·罗杰斯(ROGERSM94
(由Junio C gitsterHamano合并-- --commit 5d55554,2020 年 2 月 17 日)

config: add '--show-scope' to print the scope of a config value

Signed-off-by: Matthew Rogers

When a user queries config values with --show-origin, often it's difficult to determine what the actual "scope" (local, global, etc.) of a given value is based on just the origin file.

Teach 'git config' the '--show-scope' option to print the scope of all displayed config values.

Note that we should never see anything of "submodule" scope as that is only ever used by submodule-config.cwhen parsing the '.gitmodules' file.

config: 添加“--show-scope”以打印配置值的范围

签字人:马修·罗杰斯

当用户查询配置值与--show-origin,往往很难确定什么实际的“ scope”( localglobal给定值等)仅基于原始文件。

教 'git config' ' --show-scope' 选项打印所有显示的配置值的范围。

请注意,我们永远不应该看到任何“子模块”范围,因为只有submodule-config.c在解析“.gitmodules”文件时才会使用它。

Example:

例子:

git config -l --show-scope

global  user.global=true
global  user.override=global
global  include.path=$INCLUDE_DIR/absolute.include
global  user.absolute=include
local   user.local=true
local   user.override=local
local   include.path=../include/relative.include
local   user.relative=include

回答by AshNaz87

If you want to find to find the actual file location, it'll be in your home directory.

如果您想查找实际文件位置,它将在您的主目录中。

It is hidden and preceded with a ".".

它被隐藏并以“.”开头。

So if you're on a Mac, in your terminal you can cd ~ && open .gitconfigor open it with your favourite text editor, e.g cd ~ && atom .gitconfig.

因此,如果您使用的是 Mac,则可以在终端中cd ~ && open .gitconfig使用您喜欢的文本编辑器打开它,例如cd ~ && atom .gitconfig.