Sub topic 4
Where Settings Live: Files and Format
This chapter assumes Git is installed, you can use a terminal, and you know what a repository and a commit are. It covers the files that hold Git settings and the format they use. Later chapters cover how the scopes override each other.
The three main files
Git stores settings in plain text files. Each scope (system, global, local) maps to one file.
| Scope | Typical location | Written by |
|---|---|---|
| System | `/etc/gitconfig` (Linux, macOS with some installs) | `git config --system` |
| Global | `~/.gitconfig`, or `~/.config/git/config` | `git config --global` |
| Local | `.git/config` inside a repository | `git config --local` (the default inside a repository) |
The exact system path depends on how Git was installed. On Windows, Git for Windows keeps it under its own installation folder. On macOS, a Homebrew install uses a path under the Homebrew prefix.
Git reads the global file from ~/.gitconfig if it exists. If it does not, Git also checks $XDG_CONFIG_HOME/git/config, which is ~/.config/git/config by default. Pick one location and stay with it. If both files exist, Git reads both, which can make a setting hard to trace.
The file format
Config files use an INI-style format. A file is made of sections, and each section holds key = value lines.
[user]
name = Ada Lovelace
email = [email protected]
[core]
editor = code --wait
autocrlf = input
[init]
defaultBranch = main
[alias]
st = statusHere is how the pieces map to command-line keys:
- A section header such as
[user]names the group. - A line such as
email = [email protected]sets a variable inside that group. - The command-line key joins them with a dot:
user.email.
So git config --global init.defaultBranch main writes the defaultBranch variable into the [init] section of ~/.gitconfig. Section and variable names are case-insensitive, so defaultBranch and defaultbranch mean the same thing. Values with spaces do not always need quotes, but you should quote a value if it contains # or ;, because those characters start comments.
Subsections
Some sections have a quoted subsection name. The header [includeIf "gitdir:~/work/"] has the section includeIf and the subsection gitdir:~/work/. Subsection names are case-sensitive. A key for a subsection takes the form section.subsection.variable, for example remote.origin.url.
Seeing the file for yourself
You do not have to guess which file Git changed. Run a command that writes a value, then print the file.
git config --global user.name "Ada Lovelace"
cat ~/.gitconfigIf the file did not exist, Git creates it. You can also open it with git config --global --edit.
To see which files Git reads on your machine, and what each one contributes, use:
git config --list --show-originEach output line starts with the file path, then the setting. This confirms which of the files in the table actually exist on your system.
Common mistakes
- Editing the wrong file. Running
git config user.email ...outside a repository fails, because there is no local file. Add--global. - Two global files. Having both
~/.gitconfigand~/.config/git/configsplits your settings across two places. - Unquoted comment characters. A value with
#that is not quoted gets cut off at that character. - Hand-editing syntax errors. A line outside any section, or a missing
], makes Git stop with a "bad config line" error that names the file and line number.
Exercise
You run git config --global core.editor "code --wait". Which section of which file is changed, and what does the line look like?
Show answer
The [core] section of the global file (~/.gitconfig or ~/.config/git/config). The line is editor = code --wait.
Exercise
Git reports "fatal: bad config line 4 in file /home/ada/.gitconfig". Where do you look, and what is a likely cause?
Show answer
Open line 4 of /home/ada/.gitconfig, for example with git config --global --edit. Likely causes are a key outside any [section] header or a malformed header such as a missing ].