官术网_书友最值得收藏!

Configuration targets

In this section, we will look at the different layers that can be configured. The layers are:

  • SYSTEM: This layer is system-wide and found in /etc/gitconfig
  • GLOBAL: This layer is global for the user and found in ~/.gitconfig
  • LOCAL: This layer is local to the current repository and found in .git/config

Getting ready

We will use the jgit repository for this example; clone it or use the clone you already have from Chapter 1, Navigating Git, as shown in the following command:

$ git clone https://git.eclipse.org/r/jgit/jgit
$ cd jgit

How to do it...

In the previous example, we saw how we could use the command git config --list to list configuration entries. This list is actually made from three different levels of configuration that Git offers: system-wide configuration, SYSTEM; global configuration for the user, GLOBAL; and local repository configuration, LOCAL.

For each of these configuration layers, we can query the existing configuration. On a Windows box with a default installation of the Git extensions, the different configuration layers will look approximately like the following:

$ git config --list --system
core.symlinks=false
core.autocrlf=true
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

$ git config --list --global
merge.tool=kdiff3
mergetool.kdiff3.path=C:/Program Files (x86)/KDiff3/kdiff3.exe
diff.guitool=kdiff3
difftool.kdiff3.path=C:/Program Files (x86)/KDiff3/kdiff3.exe
core.editor="C:/Program Files (x86)/GitExtensions/GitExtensions.exe" fileeditor
core.autocrlf=true
credential.helper=!\"C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winst
ore.exe\"
user.name=Aske Olsson
user.email=aske.olsson@switch-gears.dk
$ git config --list --local
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
remote.origin.url=https://git.eclipse.org/r/jgit/jgit
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

We can also query a single key and limit the scope to one of the three layers, by using the following command:

$ git config --global user.email
aske.olsson@switch-gears.dk

We can set the e-mail address of the user to a different one for the current repository:

$ git config --local user.email aske@switch-gears.dk

Now, listing the GLOBAL layer user.email will return aske.olsson@switch-gears.dk, listing LOCAL gives aske@switch-gears.dk, and listing user.email without specifying the layer gives the effective value that is used in the operations on this repository, in this case, the LOCAL value aske@switch-gears.dk. The effective value is the value, which takes precedence when needed. When two or more values are specified for the same key, but on different layers, the lowest layer takes precedence. When a configuration value is needed, Git will first look in the LOCAL configuration. If not found here, the GLOBAL configuration is queried. If it is not found in the GLOBAL configuration, the SYSTEM configuration is used. If none of this works, the default value in Git is used.

In the previous example, user.email is specified in both the GLOBAL and LOCAL layers. Hence, the LOCAL layer will be used.

How it works...

Querying the three layers of configuration simply returns the content of the configuration files: /etc/gitconfig for system-wide configuration, ~/.gitconfig for user-specific configuration, and .git/config for repository-specific configuration. When not specifying the configuration layer, the returned value will be the effective value.

There's more...

Instead of setting all the configuration values on the command line by the key value, it is possible to set them by just editing the configuration file directly. Open the configuration file in your favorite editor and set the configuration you need, or use the built-in git config -e repository to edit the configuration directly in the Git-configured editor. You can set the editor to the editor of your choice either by changing the $EDITOR environment variable or with the core.editor configuration target, for example:

$ git config --global core.editor vim
主站蜘蛛池模板: 安康市| 临泉县| 贡山| 新乐市| 泗阳县| 小金县| 松桃| 沅陵县| 班玛县| 无为县| 堆龙德庆县| 揭东县| 榆林市| 阿拉善右旗| 田东县| 桂东县| 神木县| 峨眉山市| 定结县| 汶川县| 舞阳县| 建宁县| 芦溪县| 乌恰县| 萨迦县| 那坡县| 吉安县| 时尚| 青神县| 乳山市| 黑山县| 北安市| 康马县| 高安市| 陇南市| 无为县| 开阳县| 车险| 称多县| 霍邱县| 佛山市|