Chapter 2. Configuration

In this chapter, we will cover the following topics:

  • Configuration targets
  • Querying the existing configuration
  • Templates
  • A .git directory template
  • A few configuration examples
  • Git aliases
  • The refspec exemplified

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
[email protected]
$ 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
[email protected]

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

$ git config --local user.email [email protected]

Now, listing the GLOBAL layer user.email will return [email protected], listing LOCAL gives [email protected], 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 [email protected]. 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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset