Close

Git - Configurations

[Updated: Dec 3, 2018, Created: Dec 1, 2018]

Git can be customized via git config command.
We have used following command in our getting started tutorial to set mandatory "user.name" and "user.email" configuration:

git config --global user.name "Joe"
git config --global user.email "joe@example.com"

Above example shows how to set configuration on global level (--global).

Configuration levels

There are three configuration levels: --global, --system and --local.

--global

These configuration properties are specific to the currently logged in operating system user.
These are stored in user's home directory in ~/.gitconfig or ~/.config/git/config file (in widows, user home is C:\Users\userName\).

Let's see what is the content of this file (we are using git-bash):

Joe@jpc MINGW64 ~
$ cat ~/.gitconfig
[user]
        name = Joe
        email = joe@example.com
[alias]
        root = !pwd

We can use --list option of git config to see the configured values for a specific level:

Joe@jpc MINGW64 ~
$ git config --global --list
user.name=Joe
user.email=joe@example.com
alias.root=!pwd

We can also use --show-origin option to see the location of the file where target configurations are written:

Joe@jpc MINGW64 ~
$ git config --global --list --show-origin
file:C:/Users/Joe/.gitconfig    user.name=Joe
file:C:/Users/Joe/.gitconfig    user.email=joe@example.com
file:C:/Users/Joe/.gitconfig    alias.root=!pwd

--system

These configurations are applied across all users and all their repositories.

These are stored in /etc/gitconfig file.

Let's use git config for this level:

Joe@jpc MINGW64 ~
$ git config --system --list --show-origin
file:D:/Program Files/Git/mingw64/etc/gitconfig http.sslcainfo=D:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
file:D:/Program Files/Git/mingw64/etc/gitconfig http.sslbackend=openssl
file:D:/Program Files/Git/mingw64/etc/gitconfig diff.astextplain.textconv=astextplain
file:D:/Program Files/Git/mingw64/etc/gitconfig filter.lfs.clean=git-lfs clean -- %f
file:D:/Program Files/Git/mingw64/etc/gitconfig filter.lfs.smudge=git-lfs smudge -- %f
file:D:/Program Files/Git/mingw64/etc/gitconfig filter.lfs.process=git-lfs filter-process
file:D:/Program Files/Git/mingw64/etc/gitconfig filter.lfs.required=true
file:D:/Program Files/Git/mingw64/etc/gitconfig credential.helper=manager
file:D:/Program Files/Git/mingw64/etc/gitconfig core.editor=nano.exe

Where D:/Program Files/Git/mingw64/ is the location I installed Git distribution.

--local

These configuration are applied to current repository.

These are stored in .git/config.

We need to be in a repository to use git config for this level:

Joe@jpc MINGW64 ~/example
$ git config --local --list --show-origin
fatal: --local can only be used inside a git repository

Let's create a repository and then use config command:

Joe@jpc MINGW64 ~/example
$ git init
Initialized empty Git repository in C:/Users/Joe/example/.git/

Joe@jpc MINGW64 ~/example (master)
$ git config --local --list --show-origin
file:.git/config        core.repositoryformatversion=0
file:.git/config        core.filemode=false
file:.git/config        core.bare=false
file:.git/config        core.logallrefupdates=true
file:.git/config        core.ignorecase=true

Reading/Writing configuration

As we have seen before we can use following general format to write new values:

git config --<the-level> <key> <value>

To read:

git config --<the-level> <key>

Example:

Joe@jpc MINGW64 ~/example (master)
$ git config --local xyz.aaa "abc"

Joe@jpc MINGW64 ~/example (master)
$ git config --local xyz.aaa
abc

When writing, the option --local is redundant, by default configurations are written to local repository. Let's try without --local:

Joe@jpc MINGW64 ~/example (master)
$ git config temp.key "tempValue"

Joe@jpc MINGW64 ~/example (master)
$ git config temp.key
tempValue

Joe@jpc MINGW64 ~/example (master)
$ git config --local --list
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
xyz.aaa=abc
temp.key=tempValue

Git configs need sections

There has to be a section with each configuration value.

Joe@jpc MINGW64 ~/example (master)
$ git config newKey "newValue"
error: key does not contain a section: newKey

The git config values are grouped by sections. That means each group of section starts with square brackets. Let's read the config file directly:

Joe@jpc MINGW64 ~/example (master)
$ cd .git

Joe@jpc MINGW64 ~/example/.git (GIT_DIR!)
$ cat config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        ignorecase = true
[xyz]
        aaa = abc
[temp]
        key = tempValue

Removing configs

To remove a specific key, we can use --unset option. To remove all keys related to a section, we can use --remove-section:

Joe@jpc MINGW64 ~/example (master)
$ git config --local --list
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
xyz.aaa=abc
xyz.bbb=stu
temp.key=tempValue
temp.key2=tempValue2

Joe@jpc MINGW64 ~/example (master)
$ git config --local --unset xyz.aaa

Joe@jpc MINGW64 ~/example (master)
$ git config --local --remove-section temp

Joe@jpc MINGW64 ~/example (master)
$ git config --local --list
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
xyz.bbb=stu

See Also