Sub topic 1
Where Git configuration lives: system, global, local and worktree
Prerequisites
This chapter assumes you can use a terminal and run the basic Git workflow: clone, commit, branch, push and pull. You don't need to know any configuration syntax yet. Later chapters cover the commands for reading and writing settings in detail.
Four places, one merged result
Git doesn't keep its settings in one file. It reads up to four files, called scopes, and merges them. Each scope has a different reach.
| Scope | Typical file | Applies to | Write with |
|---|---|---|---|
| System | `/etc/gitconfig` (on Windows, inside the Git install folder) | Every user on the machine | `git config --system` |
| Global | `~/.gitconfig` or `~/.config/git/config` | Every repository of your user account | `git config --global` |
| Local | `.git/config` in the repository | That repository only | `git config --local` (the default inside a repository) |
| Worktree | `.git/config.worktree` | One working tree of a repository | `git config --worktree` |
Writing to the system scope usually needs administrator rights. Outside a repository, git config has no local scope to write to, so you must name a scope such as --global.
If both ~/.gitconfig and ~/.config/git/config exist, Git reads both. Pick one location and stay with it, or you will lose track of which file holds a setting.
Which scope wins
Git reads the scopes from the widest to the narrowest. A narrower scope overrides a wider one. The order is system, then global, then local, then worktree.
Suppose your global file sets user.email to your personal address and a repository's local file sets it to a work address. Inside that repository Git uses the work address. Everywhere else it uses the personal one.
Two other sources sit above all four files. The -c option overrides a setting for a single command, for example git -c core.editor=nano commit. Environment variables such as GIT_AUTHOR_EMAIL override identity settings.
Some keys can hold several values, such as remote.origin.fetch. For these keys, values from different scopes add up instead of replacing each other. Single-valued keys such as user.email use only the last value Git reads.
See the scopes in action
Run this inside any repository:
git config --list --show-scope --show-originEach line shows the scope, the file and the key with its value. A key can appear more than once. The last occurrence is the one in effect. To ask for the final value of one key, run:
git config --get user.email--show-scope needs Git 2.26 or later. --show-origin is available in older versions too.
Worktree configuration
A linked worktree is an extra working directory created with git worktree add. All worktrees of a repository share the same .git/config. If you want a setting to differ between worktrees, you must opt in first:
git config extensions.worktreeConfig true
git config --worktree user.email "[email protected]"The first command turns on reading of per-worktree files. The second writes the setting to that worktree's own config.worktree file. In a linked worktree, that file sits under .git/worktrees/<name>/ in the main repository.
Without extensions.worktreeConfig, Git ignores config.worktree. In that case git config --worktree writes to the shared local file, which defeats the purpose. Most people never need this scope. Reach for it only when two worktrees of the same repository need different values.
Editing files directly
The files are plain text in an INI-like format, with [section] headings and key = value lines. You can edit them by hand. git config --global --edit opens the global file in your editor. Hand edits are fine, but a typo in a section header can make Git refuse to run, so check with git config --list afterwards.
Exercise
A repository's .git/config sets user.email = [email protected], and ~/.gitconfig sets user.email = [email protected]. Which address does a commit in that repository use, and which command shows which file each value came from?
Show answer
The commit uses [email protected], because the local scope overrides the global one. Run git config --list --show-origin (or git config --show-origin --get user.email) to see the file behind each value.
Exercise
You ran git config --worktree core.editor nano in a linked worktree, but the setting appears in the shared .git/config. What did you forget?
Show answer
You did not enable extensions.worktreeConfig first. Run git config extensions.worktreeConfig true, then set the value again with --worktree.