Where do the settings in my Git configuration come from?

社会主义新天地 提交于 2019-11-25 18:58:05

Git checks 4 places for a configuration file:

  1. Your machine's system .gitconfig file.
  2. Your user .gitconfig file located at ~/.gitconfig.
  3. A second user-specific configuration file located at $XDG_CONFIG_HOME/git/config or $HOME/.config/git/config.
  4. The local repo's config file .git/config.

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

  1. System config.
  2. User config.
  3. Repo-specific config.

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 repo-specific file has defined by opening up the file .git/config for that repo.

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

From the documentation for git config:

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

  • $(prefix)/etc/gitconfig

    System-wide configuration file.

  • $XDG_CONFIG_HOME/git/config

    Second user-specific configuration file. If $XDG_CONFIG_HOME is not set or empty, $HOME/.config/git/config will 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 config will 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-all and --unset. git config will only ever change one file at a time.

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

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

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)

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 --list flag) 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.

The git config man page will now indicates:

--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).

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 commented by wisbucky:

git config --show-origin --get-all core.autocrlf      file:"D:\\prgs\\git\\latest\\mingw64/etc/gitconfig"     true     file:C:/Users/vonc/.gitconfig   false 

After having previously installed Git for Windows and subsequently uninstalling it, I found that there is a configuration file installed at C:\Users\All Users\Git\config which is a system level config 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

git config --system --edit 

it would show me the system config file located at mingw32/etc/gitconfig, but 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

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)

Git config -l shows all inherited values from: system, global and local.

So you have another config file somewhere that is being loaded along with your user defined .gitconfig

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

Configuration files priority in Git for Windows:

...

$PROGRAMDATA/Git/config::
(Windows-only) System-wide configuration file shared with other Git implementations. Typically $PROGRAMDATA points 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. --system will select this file.

$XDG_CONFIG_HOME/git/config::
Second user-specific configuration file. If $XDG_CONFIG_HOME is not set or empty, $HOME/.config/git/config will 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.

...

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

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

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.

What is $(prefix)?

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

On Windows 7  (maybe same or similar for Windows 10) for Visual Studio and git command line, your global config is in

%USERPROFILE%\.gitconfig

(dot is in front of the file)

but this is not honored by SourceTree at least in Git Embedded mode, and the config is in 

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

(no dot in front of the file)

[I needed to update both files to modify my global git settings for git command and SourceTree]

Another fun part. Git hooks config was working from the AppData\Local\... location, but after more research via procmon, I noticed somehow SourceTree is also loading global from company mapped drive for my user. This makes very little sense as very few applications lookup this location, somehow ST does so if you can't make to work per location settings at ST, run procmon.exe and create a rule to log only path containing gitconfig, and you can find where the really your global conf is in case of network mapped user directory. And this may not be even fault of ST, as I see now as I write this that git.exe is loading that, but this happens only for git.exe executed by ST, while direct command line git uses %USERPROFILE%\.gitconfig

Finally I took all the results from procmon, feed into SQL and run a query to get distinct results (no particular execution order just sorted by path):

I don't know how those configs 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 command line with git seems to work fine with %USERPROFILE%\.gitconfig and that is not on this list but would look like this (on Win7) c:\Users\pawel.cioch\.gitconfig

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 .gitconfig or open it with your favourite text editor, e.g cd ~ && atom .gitconfig.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!