Git Configuration: Levels, Conditional Includes, One-Off Settings and Useful Defaults
Master git config: the file format, levels from system to worktree, reading values with types, defaults and URLs, multi-valued keys, includes and conditional includes by folder, branch or remote URL, one-off settings with -c and environment variables, and a set of recommended defaults.
kvi5hnuCode Sprout team member7 min readOn this page
Article 2 set up your name, email and editor, and showed the three main configuration levels. This article goes further: how configuration files are written, the full set of levels, reading settings reliably in scripts, conditional includes for work and personal setups, temporary settings for one command, and a list of defaults worth changing, with the reasons for each.
The file format
Configuration files use a simple INI-like format. Here's a typical ~/.gitconfig:
[user]
name = Sam Lee
email = sam@example.com
[core]
editor = code --wait
[remote "origin"] # a section with a subsection
url = git@github.com:acme/trail-guide.git
fetch = +refs/heads/*:refs/remotes/origin/*
[alias]
st = status --short --branch
# comments start with # or ;
- A key's full name is
section.key, orsection.subsection.key:user.email,remote.origin.url. - Section and key names are case-insensitive, so
core.autoCRLFandcore.autocrlfare the same. Subsection names, in quotes, are case-sensitive. - Booleans accept
true/yes/on/1andfalse/no/off/0. A key with no value, justkey, means true. - Numbers accept
k,mandgsuffixes:1mis 1,048,576. - Values containing
#or;, or leading and trailing spaces, need double quotes.
You can edit the files directly, or with git config edit (--global for the global file), which opens the file in your editor.
All the levels
| Level | Option | File | Notes |
|---|---|---|---|
| system | --system | /etc/gitconfig, or inside the Git install folder on Windows | all users on the machine |
| global | --global | ~/.gitconfig or ~/.config/git/config | your user |
| local | --local | .git/config | one repository. The default for writing |
| worktree | --worktree | .git/config.worktree | one worktree, after extensions.worktreeConfig=true (article 40) |
| command | -c key=value | none | one command only |
Each later level overrides earlier ones. --file <path> reads or writes any other file, such as a team's shared settings. git config list --show-scope --show-origin shows every value and where it came from, and that's the first thing to run when a setting doesn't behave as expected.
Reading values
git init -q trail-guide && cd trail-guide
git config set core.ignoreCase yes
git config set core.bigFileThreshold 1m
echo "as written: $(git config get core.ignoreCase)"
echo "as boolean: $(git config get --type=bool core.ignoreCase)"
echo "as integer: $(git config get --type=int core.bigFileThreshold)"
echo "pull.rebase, with a default when unset: $(git config get --type=bool --default=false pull.rebase)"
echo "=== a multi-valued key"
git config set --append remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
git config set --append remote.origin.fetch '+refs/tags/*:refs/tags/*'
git config get --all remote.origin.fetch
echo "plain get returns the last value: $(git config get remote.origin.fetch)"
echo "=== settings scoped to a URL"
git config set http.https://git.acme.example.postBuffer 524288000
git config get --url=https://git.acme.example/team/app.git http.postBuffer
git config get --show-origin user.name
as written: yes
as boolean: true
as integer: 1048576
pull.rebase, with a default when unset: false
=== a multi-valued key
+refs/heads/*:refs/remotes/origin/*
+refs/tags/*:refs/tags/*
plain get returns the last value: +refs/tags/*:refs/tags/*
=== settings scoped to a URL
524288000
file:/etc/gitconfig Sam Lee
--type=bool,int,path(expands~),colorandexpiry-datenormalize values. Scripts should always ask for a type rather than parseyesoronthemselves.--defaultgives a value when the key isn't set, instead of failing.- Some keys, like
remote.<name>.fetchandinclude.path, can appear several times.get --alllists all values, andgetalone returns the last. Write extra values withset --append, and remove them withunset --all, orunset --value=<regex>for specific ones. get --regexpmatches key names:git config get --all --show-names --regexp '^alias\.'.get --url=<url>returns the value that applies to a URL, honouringhttp.<url>.*andcredential.<url>.*sections (article 21).git config getexits with status 1 when a key isn't set, which scripts can test.
The subcommands list, get, set, unset, rename-section, remove-section and edit were added in Git 2.46. Older guides use the flag forms, such as git config --get, --get-all, --unset and -l, which still work.
Includes and conditional includes
include.path pulls another file into the configuration, which is handy for sharing a team file or keeping secrets out of a dotfiles repository:
[include]
path = ~/.gitconfig-team
includeIf includes a file only when a condition holds:
| Condition | Includes when... |
|---|---|
gitdir:~/work/ | the repository is under ~/work/. The trailing / means "and everything below" |
gitdir/i:c:/work/ | the same, case-insensitive. Useful on Windows and macOS |
onbranch:release/** | the current branch matches the pattern |
hasconfig:remote.*.url:https://github.com/acme/** | the repository has a remote with a matching URL |
cd ~
cat > ~/.gitconfig <<'EOF'
[user]
name = Sam Lee
email = sam@example.com
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
[includeIf "hasconfig:remote.*.url:https://git.acme.example/**"]
path = ~/.gitconfig-acme
[includeIf "onbranch:release/**"]
path = ~/.gitconfig-release
EOF
printf '[user]\n email = sam.lee@acme.example\n' > ~/.gitconfig-work
printf '[core]\n abbrev = 12\n' > ~/.gitconfig-acme
printf '[commit]\n verbose = true\n' > ~/.gitconfig-release
mkdir -p work personal
git init -q work/app && git init -q personal/blog
echo "work/app email: $(git -C work/app config get user.email)"
echo "personal/blog email: $(git -C personal/blog config get user.email)"
git -C personal/blog remote add origin https://git.acme.example/team/blog.git
echo "blog with an acme remote, core.abbrev: $(git -C personal/blog config get core.abbrev)"
git -C personal/blog switch -q -c release/1.0
echo "on release/1.0, commit.verbose: $(git -C personal/blog config get commit.verbose)"
work/app email: sam.lee@acme.example
personal/blog email: sam@example.com
blog with an acme remote, core.abbrev: 12
on release/1.0, commit.verbose: true
- Put the
includeIfsections after the defaults they override, because later values win. gitdir:patterns are matched against the.gitfolder's path as you reached it, and also against its real path with symbolic links resolved. Paths with..aren't normalized, so write them plainly, with~or absolute paths.hasconfig:remote.*.url:(Git 2.36) chooses settings by where the code is hosted, which works wherever you clone. The included file can't itself define remotes.onbranch:patterns use**for any depth, like.gitignore.
One-off settings
To change a setting for a single command, without touching any file, use -c before the subcommand:
git -c core.pager=cat log -3 # no pager, just this once
git -c user.email=bot@example.com commit -m "Automated update"
git -c advice.detachedHead=false switch --detach v1.0
git -c http.sslVerify=false clone https://... # only for a known test server!
Environment variables do the same for scripts and CI:
| Variable | Effect |
|---|---|
GIT_CONFIG_COUNT, GIT_CONFIG_KEY_<n>, GIT_CONFIG_VALUE_<n> | set any keys, like -c, without putting values on the command line |
GIT_CONFIG_GLOBAL=<file> | use another file as the global config, or /dev/null for none |
GIT_CONFIG_NOSYSTEM=1 | skip the system config |
GIT_AUTHOR_NAME, GIT_COMMITTER_EMAIL and similar | override the identity for commits |
GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.name GIT_CONFIG_VALUE_0="Release Bot" git commit -m "Release 2.0"
GIT_CONFIG_COUNT is useful for secrets, such as an http.extraHeader with a token, because unlike -c they don't show in process listings.
Useful defaults
Git's defaults favour backward compatibility. These settings, all safe, make everyday work smoother. Each line says why:
[init]
defaultBranch = main # new repositories start on main (the default from Git 3.0)
[pull]
rebase = true # pull replays your commits instead of merging (article 19)
[push]
autoSetupRemote = true # first push of a new branch sets its upstream (article 20)
useForceIfIncludes = true # make --force-with-lease safer
[fetch]
prune = true # drop remote-tracking branches deleted on the server (article 23)
[rebase]
autoSquash = true # honour fixup! commits in rebase -i (article 15)
autoStash = true # stash and restore local changes around a rebase
updateRefs = true # move stacked branches along during a rebase
[merge]
conflictStyle = zdiff3 # show the base version in conflicts (article 13)
[rerere]
enabled = true # remember and reuse conflict resolutions
[diff]
algorithm = histogram # often more readable diffs
colorMoved = default # moved lines shown in their own colours
[commit]
verbose = true # show the diff in the commit message editor
[branch]
sort = -committerdate # git branch lists recent branches first
[tag]
sort = version:refname # v1.10 sorts after v1.9
[help]
autocorrect = prompt # offer to run the corrected command on typos
[column]
ui = auto # list branches and tags in columns
Not everything belongs in everyone's file: pull.rebase is a team choice, and rerere occasionally reuses a resolution you didn't want (check with git diff before committing). But each one is used widely.
Performance settings for large repositories
[core]
fsmonitor = true # a file system watcher speeds up git status (macOS, Windows)
untrackedCache = true
[feature]
manyFiles = true # a bundle of settings for repositories with many files
[fetch]
writeCommitGraph = true # keep the commit-graph current after each fetch
git maintenance start schedules background tasks, such as prefetching, repacking and updating the commit graph, that keep large repositories fast. scalar, installed with Git, applies these settings and more for very large repositories (article 44).
What's current
- The
git configsubcommands (list,get,set,unset,edit) arrived in Git 2.46, and are the recommended forms. - Git 3.0 will change several defaults, including
mainas the initial branch name, the reftable reference format and, as planned, SHA-256 for new repositories. Recent versions show hints about the branch name change. help.autocorrect=promptasks before running a corrected command. Since Git 2.49, a value of1means "run immediately" rather than "after 0.1 seconds".
Common questions
Which value wins when a key is set in several places?
The one read last: system, then global, then local, then worktree, then -c and environment variables. Within a file, the last occurrence wins. git config list --show-origin shows them all in order.
How do I share configuration with my team?
Git doesn't read configuration from the repository's files automatically, for security: a cloned repository could otherwise run commands. Commit a file such as .gitconfig-team, and ask everyone to add include.path pointing to it, or to run a setup script. Settings that affect files, such as line endings, belong in .gitattributes, which is shared (article 35).
How do I remove a setting?
git config unset <key>, with --global for the global file. For a key with several values, unset --all. To remove a whole section, git config remove-section <name>.
My includeIf "gitdir:..." doesn't apply. Why?
Common causes: the path lacks the trailing /, so it matches only that exact repository. Symbolic links: the pattern uses one path, but the repository was opened through another that doesn't match it, either as reached or as resolved. Or the include comes before the setting it should override. git config get --show-origin user.email in the repository shows which file the value came from.
Summary
- Config files are INI-like. Section and key names are case-insensitive. Booleans and sizes have flexible spellings, so read them with
--type. - Levels, from lowest to highest priority: system, global, local, worktree, then
-cand environment variables. git config getsupports--all,--default,--type,--regexpand--url.set --appendandunset --allmanage multi-valued keys.include.pathandincludeIfwithgitdir:,onbranch:andhasconfig:remote.*.url:select settings by folder, branch or hosting.git -c key=valueandGIT_CONFIG_COUNT/KEY_n/VALUE_nset values for one command.- A small set of defaults, such as
push.autoSetupRemote,fetch.prune,merge.conflictStyle=zdiff3andrebase.autoSquash, removes much everyday friction.
Practice questions
All repositories under ~/clients/acme/ should use your Acme email, and all others your personal email. How do you set it up?
Answer
In ~/.gitconfig, set the personal email as the default, then add a conditional include after it:
[user]
email = sam@example.com
[includeIf "gitdir:~/clients/acme/"]
path = ~/.gitconfig-acme
And in ~/.gitconfig-acme, set [user] email = sam.lee@acme.example. Check from inside a repository with git config get --show-origin user.email.
A script reads git config core.ignorecase and compares it with "true", but on some machines it's "yes" and the script breaks. What's the fix?
Answer
Ask Git to normalize the value: git config get --type=bool core.ignoreCase always prints true or false. Add --default=false so the command succeeds when the key isn't set.
How do you run one git log without the pager, without changing any configuration file?
Answer
git -c core.pager=cat log, or git --no-pager log. The -c option applies only to that command.
Previous: Searching and Statistics: git grep, shortlog, .mailmap and Repository Numbers
Next: Git Aliases: Shortcuts, Shell Aliases and a Useful Collection

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.