Git Config: A Tiny Reference
Just a small guide to git config..
What is git config?
In short, git config is a set of commands which allow you to set/access git configuration variables. These variables can be stored at three different levels:
- Local (Current repo)
- Global (Current logged-in system user)
- System (At the system level).
git config lets you get and set configuration variables that control all aspects of how git looks and behaves.
If no scope is specified: local is the default, given that you are currently within a git repository directory where a
.git
folder is present ,or else the default is global.
Why should you care?
According to the Atlassian git documentation:
“Becoming familiar with git config and the various Git configuration settings will help you create a powerful, customized Git workflow.”
By having basic understanding of git config and its commands; you can make your workflow smoother and more predictable, especially when working in teams.
git config files
For each level; there are corresponding git config files. I made the following table to summarize the basics around the files at each level:
git config commands
The following is a list of common git config commands:
Please note the following:
Local git variables are project specific. So, if you would like to access all git config variables, including the project ones; cd to the repo directory.
git config Variables
Let’s examine (and modify) a typical local git config file.
- Start by creating creating a project directory:
mkdir myproj
2. Initialize a git repository there:
git init
This will create a hidden .git folder pertaining to your project.
If your using linux; the .git folder will not be listed when you run the command ls
. Instead; run ls -a
3. Navigate to .git/gitconfig:
A basic text file.Here’s how mine looks like:
Here we can see the first, most fundamental git config section: core
.Under which we can see the corresponding variables.
4. Making modifications to the file:
The git config is always modified as a response to certain changes in the project. These changes are automatic. An example of that is running
git push:
There are two ways of manually editing the git config files with cli:
- Using the following command:
git config section.variablename “new variable value”
Example:
git config core.filemode true
Example:
git config --global user.name “Ammar Halees”
- Using the nano gnu editor:
git config--scope--edit
Please note the following:
Git config variables are cascading; meaning that project-level variables override global-level ones (since they’re more specific to that project). And global-level ones override system-level ones.
Extra : git config File Syntax
You can find more about the syntax here.
The basic structure of a git config file is as follows:
1. The file is divided into sections:
- A section begins with the name of the section in square brackets and continues until the next section begins.
- Section names are case-insensitive. Only alphanumeric characters,
-
and.
are allowed in section names.
Each variable must belong to some section, which means that there must be a section header before the first setting of a variable.
Sub-sections:
Sections can be further divided into subsections. To begin a subsection put its name in double quotes, separated by space from the section name, in the section header:
[section "subsection"]
We can see that in the figure above where: remote
is the section name and origin
is the sub-section
2. Sections contain variables:
Variables such as name
and email
for the user section
Variable names adhere to the following:
- Variable names are case-insensitive
- Allow only alphanumeric characters and
-
, and must start with an alphabetic character. - Some variables may appear multiple times; we say then that the variable is multi-valued.