Sub topic 8
Where Git stores configuration
This chapter assumes you have installed Git and can run git --version. It also assumes you have set user.name and user.email once with git config --global. Here you will learn what that command did: which file it changed, what other files Git reads, and how Git decides which value wins when the same setting appears in more than one place.
Why scopes exist
Some settings belong to a whole machine, some to you as a person, and some to a single project. Your name should follow you into every repository. A project's remote URL should stay with that project. A line-ending rule needed by one operating system should not affect another.
Git handles this with configuration scopes. Each scope is a separate file with its own reach. Git reads the files in a fixed order, and the more specific scope overrides the broader one.
| Scope | Applies to | Default file location |
|---|---|---|
| system | Every user on the machine | `<git-prefix>/etc/gitconfig` |
| global | Your user account, in every repository | `~/.gitconfig` or `~/.config/git/config` |
| local | One repository | `.git/config` inside that repository |
| worktree | One linked worktree of a repository (optional) | `.git/config.worktree` |
| command | A single Git invocation | `git -c key=value ...` or environment variables |
The system, global, and local scopes cover nearly all everyday use. Worktree and command scopes are described later in this chapter.
Precedence: the most specific scope wins
Git reads configuration in this order: system, then global, then local, then worktree, then command line. If a key is set in several places, the value read last is the one Git uses.
Suppose your global file sets user.email to your personal address, and a work repository's local file sets it to your work address. Commits in that repository use the work address. Every other repository uses the personal one. You never switch anything by hand.
The file locations in detail
System
The system file is shared by every account on the machine. Its path depends on how Git was installed:
- Linux with a distribution package: usually
/etc/gitconfig. - macOS with Homebrew:
/opt/homebrew/etc/gitconfigon Apple silicon,/usr/local/etc/gitconfigon Intel. - Git for Windows:
C:\Program Files\Git\etc\gitconfig.
Writing to this file normally needs administrator rights, and most people never edit it. The Git for Windows installer does put a few settings here, such as the credential helper and some line-ending defaults. If you see settings you never wrote, check this file first.
Global
The global file belongs to your user account. Git looks in two places:
~/.gitconfig, the traditional location. On Windows,~is your user profile folder, for exampleC:\Users\you.$XDG_CONFIG_HOME/git/config, which is~/.config/git/configwhenXDG_CONFIG_HOMEis not set.
If both files exist, Git reads both. The XDG file is read first and ~/.gitconfig after it, so ~/.gitconfig wins on conflicts. When you run git config --global key value, Git writes to ~/.gitconfig. The one exception is when only the XDG file exists. In that case Git writes there.
Having two possible files causes a common confusion. You edit one file, but a value you thought you removed comes from the other. The --show-origin option, covered below, settles that quickly.
Local
The local file is .git/config inside the repository. It exists only after git init or git clone. Git creates it with a few bookkeeping entries, and it also stores remotes and branch tracking information. It is not part of your project history. Git does not commit it, and it is not copied when someone else clones the repository.
That last point matters for safety. A teammate who clones your repository does not receive your .git/config, so they cannot pick up your local settings. It also means a setting you want everyone to share cannot go in this file. Use a tracked file in the repository instead.
Worktree
A linked worktree is an extra working directory attached to the same repository, created with git worktree add. Worktree scope lets one worktree override settings that would otherwise be shared. It is off by default. To use it, enable extensions.worktreeConfig, then write with git config --worktree. You can skip this scope until you use multiple worktrees.
Command line
git -c name=value <command> sets a value for one invocation only. Nothing is written to disk:
git -c core.editor=nano commit
git -c user.email="[email protected]" log -1 --format="%ae"Git also reads some settings from environment variables, such as GIT_AUTHOR_EMAIL. Those override file-based values for the author fields.
The file format
Every scope uses the same plain-text format, so you can read and edit it by hand. A typical global file looks like this:
[user]
name = Your Name
email = [email protected]
[init]
defaultBranch = main
[core]
editor = nano
[alias]
st = status --shortText in square brackets is a section. Each indented key = value line is a variable in that section. The key user.name means variable name in section user.
Some keys have a subsection in quotes. This is how remotes are stored in a local file:
[remote "origin"]
url = https://example.com/team/project.git
fetch = +refs/heads/*:refs/remotes/origin/*The key for that URL is remote.origin.url. Section names and variable names are not case sensitive, so init.defaultBranch and init.defaultbranch are the same key. Subsection names are case sensitive, so remote.Origin.url is a different key from remote.origin.url.
Lines starting with # or ; are comments.
Reading configuration
List and read values
git config --list
git config user.emailThe first command prints every value Git can see from the current directory. Inside a repository, that includes the local file. Outside a repository, it shows only system and global values. The second prints the effective value of one key.
To read from one scope only, name it:
git config --global --list
git config --system --list
git config --local --list--local works only inside a repository. Outside one, Git stops with a message that --local can only be used inside a git repository.
Find where a value comes from
Two options show provenance. Use them whenever a setting behaves unexpectedly:
git config --list --show-origin
git config --list --show-scope
git config --show-origin --show-scope user.email--show-origin prints the file path in front of each entry. --show-scope prints the scope name (system, global, local, worktree, or command). --show-scope needs Git 2.26 or later. The output looks like this, with a tab between the origin and the entry:
file:/home/you/.gitconfig user.name=Your Name
file:/home/you/.gitconfig [email protected]
file:.git/config core.repositoryformatversion=0When the same key appears more than once in a --list output, the last entry is the one in effect.
Writing configuration
By default, git config key value writes to the local file. That default throws people off, because earlier in the tutorial you used --global. Outside a repository, the same command without a scope flag fails.
Try this in a scratch directory to see two scopes interact:
mkdir scope-demo && cd scope-demo
git init
git config user.email "[email protected]"
git config --show-origin --show-scope user.email
git config --global --get user.emailThe second command writes to .git/config, since you gave no scope. The third shows the local value and its source. The fourth reads only the global value, which is still your personal address. Git uses the local value inside scope-demo, and the global value everywhere else.
To remove the local override and fall back to the global value:
git config --unset user.email
git config --show-origin user.emailAfter the unset, the second command prints the global entry again.
Editing the file directly
You can open a scope's file in your editor:
git config --global --edit
git config --local --editThis is useful for deleting several entries or adding comments. Keep the [section] structure intact. A syntax error stops many Git commands with a message that names the file and line number.
Settings with several values
A few keys can appear many times, such as remote.origin.fetch or credential.helper. For those, use --add to append and --get-all to read every value:
git config --global --add credential.helper cache
git config --get-all credential.helperA plain git config key value on a key that already has several values fails with a warning about multiple values. Use --replace-all to overwrite them deliberately, or --unset-all to remove them.
Different identities in different places
A common real-world need is a work email for work projects and a personal email for everything else. You could set user.email locally in each work repository, but you will forget one. A conditional include solves it once.
Put this in your global file:
[user]
name = Your Name
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-workThen create ~/.gitconfig-work:
[user]
email = [email protected]Any repository whose .git directory is under ~/work/ now loads the second file after the main one, so its email wins. The trailing slash in gitdir:~/work/ matters, because it makes the pattern match everything below that directory. You can check it from inside a work repository with git config --show-origin user.email.
Overriding where Git looks
Environment variables can redirect the files Git reads. This is useful for tests and for reproducing bug reports on a clean setup:
GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 git config --listGIT_CONFIG_NOSYSTEM=1 tells Git to skip the system file. GIT_CONFIG_GLOBAL replaces the global files with the path you give, and /dev/null means an empty file. GIT_CONFIG_GLOBAL needs Git 2.32 or later. On Windows with PowerShell, set the variables first with $env:GIT_CONFIG_NOSYSTEM = "1".
Common errors
- Commits show the wrong name or email. A local file, an included file, or an environment variable overrides your global value. Run
git config --show-origin --show-scope user.emailand look at the scope. - A value you removed comes back. You removed it from one scope, but another scope still sets it. Run
git config --list --show-originand check every file shown. - Both
~/.gitconfigand~/.config/git/configexist. Git reads both, and settings in~/.gitconfigwin. Pick one location and delete the other. --localfails with a "not in a git repository" message. You are outside a repository. Move into one, or choose--global.- A setting you wrote in a repository did not reach a teammate.
.git/configis not shared. Put shared rules in a tracked file such as.gitattributes, or document the commands each person should run. - Credentials appear in a config file. A URL like
https://user:token@host/...stored inremote.origin.urlis visible to anyone who can read the file. Use a credential helper instead, and never copy a.git/configinto a public place.
Summary
Git's configuration lives in plain text files at several scopes. System settings apply machine-wide, global settings apply to your account, local settings apply to one repository, and the worktree and command-line scopes refine them further. Git reads them from broadest to most specific, and the last value read wins. You can inspect all of this with git config --list --show-origin --show-scope, and you choose where to write with --system, --global, --local, or --worktree.
Exercise
You run git config --global user.email "[email protected]". Inside a repository, git log still shows commits authored with [email protected]. Which command shows where that value comes from, and which scope would you suspect first?
Show answer
Run git config --show-origin --show-scope user.email. Suspect the local scope first, because a user.email entry in the repository's .git/config overrides the global value. An included file loaded by includeIf or a GIT_AUTHOR_EMAIL environment variable are the next suspects.
Exercise
In a new repository, you run git config core.editor nano with no scope flag. Which file does Git change, and will the setting apply in other repositories?
Show answer
Git writes to the local file, .git/config in the current repository. The setting does not apply to other repositories. To make it apply everywhere, add --global.
Exercise
Both ~/.gitconfig and ~/.config/git/config set core.editor, to vim and nano respectively. Which value does Git use, and why?
Show answer
Git uses vim. It reads the XDG file (~/.config/git/config) first and ~/.gitconfig after it. The later value overrides the earlier one.