Configure Git like a Pro (Meet Git #3)

Introduction to git configuration

Krystian Szpiczakowski
7 min readOct 13, 2022
Photo by Gabriel Heinzer on Unsplash

Hello and welcome to the next episode of my Git series, devoted not necessarily developers.

Previously¹, I was showing you how to install Git step by step on your computer. Today, I’m going to do a brief introduction to Git configuration.

Why to configure?

You may wonder what to configure, as you may not know how Git works at all. If that’s the case, don’t worry, because I will show you today only the absolute minimum that is required to kick off.

Git can be configured in many ways for many reasons, for instance:

  • Choose a default editor for writing commit messages
  • Instruct Git how to deal with line endings (Windows and Linux operating systems treat line endings differently, which will be explained in one of the next articles)
  • If you execute the same Git commands over and over again, it might be helpful to speed up your work by creating aliases
  • And much more, which I’ll be explaining in subsequent articles

Scopes of configuration

Before we move on, you need to know there are three scopes of configuration: system, global and local (project) settings.

System settings

System settings are on top of the hierarchy, and simply put, those are applied for all users/accounts and their Git repositories on the operating system.

If you installed Git on Windows in the default directory, then system settings should be present under the following path: C:\Program Files\Git\etc\gitconfig.

For Linux, system settings are stored in the following file: /etc/gitconfig.

To check current system settings, open Git Bash or terminal, and enter git config --list --system.

To set a system property, use git config --system <property> <value>, for instance git config --system core.autocrlf true.

System-scoped Git configuration

Global settings

Global settings are one level below compared to system settings, and they apply to the current user/account on your operating system.

Those settings are stored in the user’s directory:

  • On Windows: C:\Users\<user>\.gitconfig
  • On Linux: /home/<user>/.gitconfig

Global settings have a lower scope than system settings, and this allows you to overwrite settings coming from the system scope.

For example, if the system property core.editor has value vim, this means that all users in the operating system will use Vim as the default text editor for Git. Then, if we set a value nano for the same property core.editor, but within the global scope, the effective value of this setting will be nano. This means, that the default editor for your account is Nano instead of Vim.

To check current global settings, enter the following command in the terminal:git config --list --global.

To set a global property, use git config --global <property> <value>, for instance git config --global core.editor nano.

Vim is the default text editor in the system scope
Nano is the default text editor in the global scope (for the current user of the operating system)
Nano has been automatically launched when “git commit” executed

Project settings

Project/local scope is the lowest scope managed by Git. This means, any settings existing in the higher scopes (system and global), will be overwritten by the same property from the project scope.

Project settings are stored in a configuration file, under <git_repo>/.git/config, where git_repo is just a directory containing a .git subdirectory generated by Git (this will be covered in the next article).

To check current local settings, enter the following command in the terminal:git config --list --local.

Local configuration is stored within Git repository
Local-scoped Git configuration

Changing local configuration may be useful if you work on multiple repositories, and say, for one of them you’d like to use a different e-mail address.

To adjust the local-scoped configuration, use git config --local <property> <value>, for example git config --local user.email example@example.com. Also, you can skip the --local flag, so git config user.email example@example.com has the same effect.

If you don’t know where the setting comes from

As you can imagine, three different scopes of configuration can sometimes become a mess. If you got lost, and you’d like to investigate what’s really going on, you have two options:

  • git config --list --show-origin
  • git config --list --show-scope

The two commands are similar: the first shows next to each setting the location from which Git reads the configuration, and the second shows the scope for the given setting. This is extremely useful, especially if you wonder why Git behaves differently than you expect.

It happened to me that Git was behaving weird, and only after executing git config --list --show-origin did I understand the reason — part of my configuration was derived from the system-scoped configuration.

Show origin of each setting in Git configuration
Show scope of each setting in Git configuration

Initial configuration

Initial Git setup is very simple, and it boils down to checking the following:

  • Choose how Git is supposed to treat line endings
  • Specify username and e-mail address (this information will be included in your commits)
  • Select a default text editor

Setting #1: Handle line endings

If you have a fresh installation of Git, this setting should be already configured, regardless if you’re using Windows, Linux or Mac. Nonetheless, it’s very important to double-check if the correct value is present, because this value determines how Git will treat line endings in within text files.

Briefly, Windows uses CRLF line ending, while Linux and macOS use LF line ending, but this is a story for a separate article.

Depending on the operating system you are using:

  • For Windows, you must ensure that core.autocrlf is set to true
  • For Linux/macOS, you must ensure that core.autocrlf is set to input

In case you have invalid setting, just fix the configuration with git config, as explained earlier.

*If you are already familiar with Git, you may say this issue could (and should) be solved by editing the .gitattributes file — that’s correct, but this article is already packed with information, so I don’t want beginners to bother with another topic. I’ll come back to this one — I promise :)

Setting #2: Add username and email

This is a mandatory setting, which you must provide to start using Git. You don’t have to provide real information, generally speaking, but your username and e-mail will be included in every commit you perform. If you’re about to share your repository later on to collaborate with someone, providing real information may be useful.

To do this, enter these two commands:

  • git config --global user.name <your_name>
  • git config --global user.email <your_email>

Setting #3: Select a default editor

If you leave this setting, there’s a high likelihood that your default text editor is Vim. Some people like this tool, other hate it.

Well, the truth is that if you’ve never used Vim before, you’re going to curse because all common operations are not what you’re used to.

If you are adventurous, and you love learning (like me), go for Vim! For those who just want a simple text editor that doesn’t require additional learning, I can recommend Nano.

To set the editor, enter the following:

  • git config --global core.editor vim for Vim
  • git config --global core.editor nano for Nano
  • git config --global core.editor <path_to_your_editor> for something else

Summary

Use git config command in your terminal to manage Git-related settings on your computer:

  • To list all properties: git config --list
  • To list system properties: git config --list --system
  • To list global properties: git config --list --global
  • To list local (repository) properties: git config --list --local
  • To set a system property: git config --system <someproperty> <value>
  • To set a global property: git config --global <someproperty> <value>
  • To set a local (repository) property: git config --local <someproperty> <value>, or you can skip the --local flag

Additionally, if you want to know an explicit config location and scope, use the following:

  • To show where the setting comes from:git config --list --show-origin
  • To show to which scope the particular setting belongs: git config --list --show-scope

What’s next?

Congrats! You’ve just learned how to configure Git, so you’re ready to start using Git for real!

To be honest, I expected this article to be shorter. On the other hand, the configuration described above is the absolute minimum to start using Git, and if you’d like to learn more about Git configuration, check out the configuration chapter in an excellent book titled Pro Git².

In the next article, we will create our first Git repository and finally start doing practical stuff, so stay tuned!

References

[1] Krystian Szpiczakowski, Install Git and start using it like a Pro (Meet Git #2) https://medium.com/@kszpiczakowski/install-git-and-start-using-it-like-a-pro-meet-git-2-b3c2dd96942f

[2] Chacon, S & Straub, B 2014, Pro Git, https://git-scm.com/book/en/v2

--

--