Sub topic 5
How Scopes Override Each Other
This chapter assumes Git is installed, you are comfortable in a terminal, and you know what a repository and a commit are. It also assumes you know the three scopes: system, global and local. Here you will learn which scope wins when the same setting appears in more than one, and how to prove it.
The rule: the narrowest scope wins
Git reads its configuration files in a fixed order. Later files replace values from earlier files. The order runs from the widest scope to the narrowest:
- System: applies to every user on the machine.
- Global: applies to your user account.
- Local: applies to one repository, stored in
.git/config.
If user.email is set at all three levels, the local value is the one Git uses. If it is set only at system and global levels, the global value wins. A scope that does not set a key never blocks the others.
A worked example
Set the same key at two scopes, then inspect the result. Run the first command anywhere. Run the rest inside a repository.
git config --global user.email "[email protected]"
cd my-project
git config --local user.email "[email protected]"
git config --show-origin --get user.emailThe last command prints the effective value and the file that supplies it. The output looks like this:
file:.git/config [email protected]Now remove the local value and check again:
git config --local --unset user.email
git config --show-origin --get user.emailThe global value takes over again. The output now names your global file, such as file:/home/ada/.gitconfig.
Seeing every layer
--get returns only the winning value. To see the values that lost, list everything:
git config --list --show-originA key that appears several times in this list is set at several levels. Git reads the list top to bottom, so the last occurrence of a key is the effective one. The origin on each line shows which file it came from. This is the fastest way to find out why a setting is not what you expected.
Multi-valued keys are different
The "last value wins" rule applies to single-valued keys such as user.email and core.editor. Some keys can legitimately hold several values, such as remote.origin.fetch. For these, Git collects the values from all scopes instead of replacing them. If you are unsure, --list --show-origin shows every entry.
Common mistakes
- Setting a value globally and expecting it to apply everywhere. A local value in a repository silently overrides it. Check with
--show-origin. - Forgetting that
--globalis not the default. When you rungit config key valueinside a repository without a scope flag, Git writes to the local file. - Editing the wrong file. Check the origin first, then change the scope that actually supplies the value.
Exercise
Exercise
Your global user.email is [email protected]. Inside one repository, git config --show-origin --get user.email prints file:.git/config [email protected]. What happens to commits in that repository, and how do you make them use the global address again?
Show answer
Commits in that repository use [email protected], because the local scope overrides the global scope. To fall back to the global address, remove the local value with git config --local --unset user.email. Running the --show-origin --get command again should then show your global file.
Exercise
Which command shows every place a key is set, including values that were overridden?
Show answer
git config --list --show-origin. Each line names the file it came from. For a single-valued key, the last occurrence in the list is the effective value.