Sub topic 2
Reading and writing settings with git config
This chapter assumes you know the four configuration scopes (system, global, local, worktree) and that a later scope overrides an earlier one. It also assumes you are comfortable in a terminal. Here you will learn the git config command itself, which reads and writes those files for you.
The shape of a setting
Every setting has a key written as section.name, such as user.email. Some keys have a subsection in the middle, such as remote.origin.url or alias.st. Section and variable names ignore case. Subsection names do not.
The command has two jobs, depending on the arguments you give it:
- One argument (a key) reads the value.
- Two arguments (a key and a value) write the value.
git config user.name
git config user.name "Ada Lovelace"Choosing where to write
Without a scope flag, a write goes to the local file, .git/config. This is a common source of surprise: you set your email outside a repository and Git refuses, or you set it inside one and only that repository changes.
| Flag | File written |
|---|---|
| `--local` | `.git/config` in the current repository (the default for writes) |
| `--global` | `~/.gitconfig` or `~/.config/git/config` |
| `--system` | The machine-wide file, usually needing administrator rights |
| `--worktree` | `config.worktree` for the current linked worktree |
Reads work differently. With no flag, Git reads every applicable file and returns the winning value. With a flag, it reads only that file.
git config --global core.editor "code --wait"
git config --get core.editor
git config --local --get core.editorThe last command prints nothing and exits with status 1 if the local file has no value, even though the effective value exists globally.
Common operations
git config --list
git config --global --unset alias.st
git config --global --add safe.directory /srv/repo
git config --get-all safe.directory
git config --global --type=bool pull.rebase true
git config --global --edit--listprints every activekey=valuepair. Keys can appear more than once because each scope contributes its own line.--unsetremoves a key from one file.--addappends a value to a key that may hold several values, such assafe.directory.--get-allprints every value of a multi-valued key.--type=boolmakes Git validate the value and normalize it totrueorfalse. The older form is--bool.--editopens the chosen file in your editor. Use it for bulk changes.
Git 2.46 and later also accept git config get, set, unset and list. The older form shown here works in all versions.
Errors you will meet
- Overwriting a multi-valued key.
git config safe.directory /tmpfails with "cannot overwrite multiple values". Use--replace-allto replace them, or--addto append. - Unsetting a multi-valued key.
--unsetfails when the key has several values. Use--unset-all. - Using
--localoutside a repository. Git exits with "--local can only be used inside a git repository". - Unquoted values. A value with spaces must be quoted for your shell, as in
"code --wait". Otherwise Git receives extra arguments and fails. - Wrong scope. If a setting has no effect, another scope may be overriding it. The inspection chapter covers
--show-origin.
A worked example
git config --global user.name "Ada Lovelace"
git config --global user.email "[email protected]"
git config user.email "[email protected]"
git config --get user.email
git config --global --get user.emailThe first two commands set your identity everywhere. The third, run inside a repository, writes a local override. The fourth prints [email protected], because local wins. The fifth prints [email protected], because it reads only the global file.
Exercise
You run git config user.email "[email protected]" inside a repository. Which file changes, and what does git config --global --get user.email print afterwards?
Show answer
The local file, .git/config, changes, because writes default to the local scope. The global command still prints the old global address, because --global reads only the global file.
Exercise
git config safe.directory /tmp/new fails with "cannot overwrite multiple values". Which command appends the value without removing the existing ones?
Show answer
git config --add safe.directory /tmp/new. Use --replace-all only if you want to replace every existing value.