Sub topic 8
Where Git stores configuration
This chapter assumes you have installed Git and set your name and email with git config --global. It also assumes you can open a terminal and run commands. Here you will learn which files hold those settings, how Git decides between conflicting values, and how to find out where a value came from.
Three scopes, three files
Git reads settings from several plain-text files. Each file belongs to a scope, which decides how widely its settings apply.
| Scope | Applies to | Typical location | Flag |
|---|---|---|---|
| System | Every user on the machine | `/etc/gitconfig` on Linux. Under the Git install folder on Windows (`etc\gitconfig`). Under the Homebrew prefix on macOS. | `--system` |
| Global | Your user account, all repositories | `~/.gitconfig`, or `~/.config/git/config` | `--global` |
| Local | One repository | `.git/config` inside that repository | `--local` |
The git config --global commands from the previous chapter wrote to the global file. You did not have to create it. Git creates it the first time you set a value.
Precedence: the narrowest scope wins
When the same key appears in more than one file, Git uses the value from the narrowest scope. Local overrides global, and global overrides system.
This lets you keep one default identity globally and override it in a single project. Settings that appear in only one file simply apply as they are.
The file format
A config file is an INI-style text file with sections in brackets and key = value lines. Open your global file with cat ~/.gitconfig and you will see something like this:
[user]
name = Your Name
email = [email protected]
[init]
defaultBranch = mainThe command git config --global user.name "Your Name" means "set the key name in the section user." You can edit the file by hand, or open it in your configured editor:
git config --global --editEditing by hand is fine, but a typo in the file can make every Git command fail with a "bad config line" error. The git config command avoids that risk.
Setting and reading values by scope
Run these commands inside the demo repository from the first-commit exercise. Without a scope flag, git config writes to the local file when you are in a repository.
cd demo
git config user.email "[email protected]"
git config --get user.email
git config --global --get user.emailThe first --get prints [email protected], which is the effective value. The second prints the global value, [email protected]. Both exist at the same time. Git uses the local one inside demo.
To remove the override, delete the key from the local scope:
git config --local --unset user.email
git config --get user.emailThe effective value is now the global one again.
Finding where a value comes from
When Git behaves unexpectedly, the question is usually which file set a value. --show-origin answers it:
git config --list --show-origin
git config --show-origin --get user.emailEach line starts with the file path, followed by the setting. If a key appears more than once in the full listing, the last occurrence is the one in effect. Git 2.26 and later also offers --show-scope, which prints system, global, or local instead of a path:
git config --list --show-scopeDifferent identities for different folders
A common need is a work email for company projects and a personal email elsewhere. You can avoid setting the local value in every repository by using a conditional include in your global file:
[user]
name = Your Name
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-workPut the work settings in ~/.gitconfig-work:
[user]
email = [email protected]Any repository under ~/work/ now uses the company email. The trailing slash in gitdir:~/work/ matters. It makes Git match every repository inside that folder.
Common errors
- Permission denied with
--system. The system file is owned by an administrator. Usesudoon Linux and macOS, or an elevated terminal on Windows. Most people never need this scope. - "--local can only be used inside a git repository." You ran a local command outside a repository. Move into one, or choose
--global. - Your global change has no effect. A local value is overriding it. Run
git config --show-origin --get <key>to find the file. - Commits show the wrong email. The local scope of that repository, or an
includeIfrule, is supplying a different value. Checkgit config --show-origin --get user.emailfrom inside the repository. - Values with spaces are split. Quote them:
git config --global user.name "Your Name".
Exercise
In a repository, git config --global --get user.email prints [email protected], but git config --get user.email prints [email protected]. Which file supplied [email protected], and which command shows you its path?
Show answer
The local file, .git/config in that repository, supplied it, because the local scope overrides the global scope. git config --show-origin --get user.email prints the file path.
Exercise
You want every repository under ~/clients/ to use the email [email protected] without editing each repository. Which feature do you use, and what two pieces do you need?
Show answer
Use a conditional include. In ~/.gitconfig, add an [includeIf "gitdir:~/clients/"] section with a path pointing to a second file. Put [user] and email = [email protected] in that second file.