How to Configure targets (Git)

Jul 21, 2020

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

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

Step 1 : We will use the jgit repository for this example;

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

Step 2 : we can 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.

git config --list --global

git config --list --local

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

git config --global user.email

Step 4 : We can set the email address of the user to a different one for the current repository:

git config --local user.email devtut@gmail.com

Step 5 : Now, listing the GLOBAL layer user.email will return john.doe@example.com, listing LOCAL will give john@example.com

git config --local user.email

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.

Step 6 : 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 nano

git config -e