Introduction

In our earlier articles on using the GIT version control system, we’ve shown you how to set or change default user name and email and how to set or change the default editor. In this article, we will share some parameters that you may consider setting in your git configuration and we’ll also talk about the hierarchy of the levels of git configuration settings.

To learn how to install git on a Linux system, please do take a look at our article on how to install git on Centos where we’ve covered how to install the git version control system via yum/rpm and also how to install it from source.

 

Levels of git configuration
There are 3 levels of git config; project, global and system.

project: Project configs are only available for the current project and stored in .git/config in the project’s directory. To access the project or repository level git configuration, we execute the ‘git config’ command from within the project directory.

global: Global configs are available for all projects for the current user and stored in ~/.gitconfig. To access the global git configuration, we use the ‘git config –global’ command.

system: System configs are available for all the users/projects and stored in /etc/gitconfig. To access the system git configuration, we use the ‘git config –system’ command.

After understanding the three levels of git configuration, we’ll now show you how to set some important parameters in the global configuration.

 

Check currently set parameters:
We use the ‘git config –global –list’ command to list the currently set global git configuration parameters.

[root@linuxnix ~]# git config --global --list
 fatal: unable to read config file '/root/.gitconfig': No such file or directory
 [root@linuxnix ~]#

Since we are working on a fresh git installation, we do not have any parameters set at the moment.

 

Setting the username and email
Although we’ve talked about this at length in a separate article, we’ll repeat it again here for the sake of completeness.

[root@linuxnix ~]# git config --global user.name "Sahil Suri"
[root@linuxnix ~]# git config --global user.email "sahil.suri@linuxnix.com"
[root@linuxnix ~]#

Let’s run the ‘git config –global –list’ command now to view our newly set parameters.

[root@linuxnix ~]# git config --global --list
 user.name=Sahil Suri
 user.email=sahil.suri@linuxnix.com

As we can observe from the above output, the parameters have been set successfully. Let’s now check the .gitconfig file in our home directory to validate that the user.name and user.email have been added to the file.

[root@linuxnix ~]# cat ~/.gitconfig
[user]
name = Sahil Suri
email = sahil.suri@linuxnix.com

 

Setting the core editor
We’ve discussed this option as well in a separate article but we will configure it here again in the interest of completeness.

[root@linuxnix ~]# git config --global core.editor vim
 [root@linuxnix ~]#
 [root@linuxnix ~]# git config --global --list
 user.name=Sahil Suri
 user.email=sahil.suri@linuxnix.com
 core.editor=vim

 

Setting autocorrect
Git offers a number of commands for managing projects and it’s easy to get confused and mistype some of them if we are in a hurry. Setting autocorrect to 1 will execute the correct git command in case we misspell it.

[root@linuxnix ~]# git config --global help.autocorrect 1
[root@linuxnix ~]# git config --global --list
user.name=Sahil Suri
user.email=sahil.suri@linuxnix.com
core.editor=vim
help.autocorrect=1

 

Setting color.ui
Color coding command line output can help the output for readable and also assist in debugging. Setting color.ui to auto will allow git to color code some of the information it displays making the information more readable.

[root@linuxnix ~]# git config --global color.ui auto

Let’s verify the update.

[root@linuxnix ~]# git config --global --list
user.name=Sahil Suri
user.email=sahil.suri@linuxnix.com
core.editor=vim
help.autocorrect=1
color.ui=auto

If git detects that it’s running within a script then it will not color code information making log parsing easier.

 

Setting autocrlf
While writing code on the Windows platforms, it will denote new lines with carriage returns. In contrast to this, the Linux platform will interpret new lines as line feeds. Setting autocrlf to true will change carriage returns and line feeds to line feeds only. This is useful when doing cross-platform development.

[root@linuxnix ~]# git config --global core.autocrlf input

Let’s validate the update by checking the output ‘git config –global –list’ command and displaying the global git configuration file.

[root@linuxnix ~]# git config --global --list
user.name=Sahil Suri
user.email=sahil.suri@linuxnix.com
core.editor=vim
core.autocrlf=input
help.autocorrect=1
color.ui=auto

[root@linuxnix ~]# cat ~/.gitconfig
[user]
name = Sahil Suri
email = sahil.suri@linuxnix.com
[core]
editor = vim
autocrlf = input
[help]
autocorrect = 1
[color]
ui = auto 

Conclusion

In this article, we discussed the three levels of git configuration and we demonstrated how to set some important parameters so that your experience using git is more comfortable. We hope you found these tips useful and we look forward towards your feedback.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.