Signup/Sign In

Git Config

Git Config(short for configuration) is a command that helps us in setting various fields in Git like our name, email address, the default editor that should be used, etc. This is often done after installing Git on our system but these fields can also be changed at some other time in the future.

Configuring Git

Config File

  • We have three levels of configuration and the field values for each level are stored in a different file.

  • The three levels of configuration are local, global, and system.

  • One thing to note here is that the more specific configuration will override the less specific(more general) configuration. The order in which the configuration settings take place is Local then Global and in the end System. Let’s take a look at each one of them.

Local Git Config

We can change the configuration at the local level i.e. the current Git Repository on which we are working. To change any fields at the local level we use the --local option along with the Git Config command. These settings reside in the .git directory by the name of config. We can view these configured values using the following command.

$ git config --list --local

Viewing the local configurations

Or we can also look at the contents of the config file.

Viewing the contents of the local config file

Global Git Config

We can set some global settings for our Git Repositories. These configurations will apply to all the git repositories for a particular user of a system. We use the --global option to alter these configurations. All these settings are stored in a .gitconfig file in the home directory. Let’s take a look at these configurations.

$ git config --list --global

Viewing the global configurations

The global .gitconfig file also displays the same information.

Viewing the contents of the global .gitconfig file

System Git Config

We can also configure the Git Repositories at the system level. These settings will apply to all the Git Repositories of all users that share a system. We use the --system option to change these values. These settings are stored in the <git-install-root>\mingw64\etc\gitconfig file.

$ git config --list --system

Viewing the system configurations

Git Config Command

Git Config command can be used to set several different fields. Let’s take a look at how to use this command to configure Git.

Setting the User Name and E-Mail

We can change the user name and email address with the Git Config command. These details are also added to commits. We also need to pass the --local or --global option to specify whether we have to change the name and email of a specific directory or for all directories of a user. We can set these using the following command:

$ git config --global user.name “user-name”
$ git config --global user.email “email” 

Setting the Text Editor

A lot of times Git will open up an editor for us to enter something. A common example of Git opening a text editor is when we use the Git Commit command and do not use the -m flag to enter the message. We can set the editor that Git will open using the Config command.

$ git config --global core.editor “editor”

Setting Aliases

Some Git commands are used very frequently and it can get exhausting to write these commands again and again, especially when they are pretty long. We can set a shorter alias(another name) for a Git command using Git Config and we no longer need to write the original command. However, we can still use the original command whenever we want without removing the alias. Let’s say we want to shorten the Git Checkout command to just Git co, then the git config command would like the following:

$ git config --local alias.co checkout

Configuring Merge Tools

A merge tool is a visualization tool that allows you to compare files. Such tools come in handy when you run into a merge conflict and want to check where conflict has occurred. By default, Git uses a diff program to tell us about the conflicts but we can change it using Git Config.

$ git config --global merge.tool <merge-tool-name>

Summary

Git Config is a very powerful command with which we can set different fields to set our needs. There are three levels at which configuration can be done - local, global, or for the entire system. All the configuration settings for the three levels are stored in different files. In this tutorial, we learned how to configure the user name, email, and text editor using the Git Config command. We can also set an alias for a specific Git Command and can even change the color scheme of Git.