How is the Git config evaluated when commit-ing?

2019-02-25 10:10发布

问题:

I'm setting up My First Git Repo, and on running;

git config --list

I've noticed I've got multiple entries for certain config values;

core.autocrlf=input
[...]
core.autocrlf=false

I'm guessing these values are doubled up because they appear in more than one of the various config files (system, global, file). My question is, which of these values takes precedence? Is the config file read line by line, and the last entry taken as the one used during a commit?

回答1:

Borrowed from The MYYN, these are the three places where to find config files:

  • $GIT_DIR/config
  • ~/.gitconfig (--global)
  • $(prefix)/etc/gitconfig

Ok, imagine you globally set your email to niko.schwarz@gmail.com. Now, we make a new repo:

$ cd /tmp
$ mkdir try && cd try
$ git init
$ git config user.email niko.schwarz@s-i.ch
$ touch hi
$ git -add .
$ git commit -m 'bla'

Then, your user.email will be set to two values:

$ git config --list | grep niko.schwarz
user.email=niko.schwarz@gmail.com
user.email=niko.schwarz@s-i.ch

But if you look at the log, the email address will be set to the one specific to the repo:

$ git log | grep niko.schwarz
Author: Niko Schwarz <niko.schwarz@s-i.ch>
    Signed-off-by: Niko Schwarz <niko.schwarz@s-i.ch>

Therefore, local beats global, which is the order in which the values are listed. Now, with a bit of a leap of faith, I'd indeed assume that git config --list shows things in an order that makes latter ones take precedence.



回答2:

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

  • $GIT_DIR/config

  • ~/.gitconfig (--global)

  • $(prefix)/etc/gitconfig

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.

The .git/config file in each repository is used to store the configuration for that repository, and $HOME/.gitconfig is used to store a per-user configuration as fallback values for the .git/config file. The file /etc/gitconfig can be used to store a system-wide default configuration.

Precendence should be from the most general to the repository-specific entries.

Or more catchy (borrowed from Niko): local beats global