我的 Git 配置中的设置来自哪里? [英] Where do the settings in my Git configuration come from?

查看:18
本文介绍了我的 Git 配置中的设置来自哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到当我运行 git config -l

时,我有两个 core.autocrlf 列表

$ git config -lcore.symlinks=falsecore.autocrlf=false颜色.diff=自动颜色.状态=自动color.branch=自动颜色.交互式=真pack.packsizelimit=2ghelp.format=htmlhttp.sslcainfo=/bin/curl-ca-bundle.crtsendemail.smtpserver=/bin/msmtp.exediff.astextplain.textconv=astextplainrebase.autosquash=真用户名=名称user.email=email@example.comcore.autocrlf=真

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

这是使用 MSysGit 1.8.3,而且我还安装了 Sourcetree (Windows 7).在 Sourcetree 中,我取消选中允许 Sourcetree 修改您的全局 Git 配置文件"

解决方案

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

  1. 您机器的系统 .gitconfig 文件.
  2. 您的用户 .gitconfig 文件位于 ~/.gitconfig.
  3. 第二个特定于用户的配置文件位于 $XDG_CONFIG_HOME/git/config$HOME/.config/git/config.
  4. 本地仓库的配置文件.git/config.

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

  1. 系统配置.
  2. 用户配置.
  3. 特定于存储库的配置.

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

#系统,适用于整机和所有用户$ git config --system --list$ git config --system --edit# 用户自定义$ git config --global --list$ git config --global --edit

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

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

来自 git config 的文档:><块引用>

如果没有用--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--unset.git config 只会改变一次一个文件.

您可以通过命令行选项或通过环境变量.--global--system 选项将将使用的文件分别限制为全局或系统范围的文件.GIT_CONFIG 环境变量也有类似的效果,但是你可以指定任何你想要的文件名.

I've noticed that I have two listings for core.autocrlf when I run 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
user.email=email@example.com
core.autocrlf=true

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

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"

解决方案

Git checks four 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 repository's configuration 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 configuration.
  2. User configuration.
  3. Repository-specific configuration.

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/config for that repository.

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.

这篇关于我的 Git 配置中的设置来自哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆