Set Git Configuration Without Losing Track of Scope
You will finish with a reliable way to set Git options, see which file supplies each value, and undo the changes you made. The examples use Git 2.43.0, installed here from the git-man package. Allow about fifteen minutes. You need a shell and a Git repository for the local examples. No command below needs sudo.
The route
Jump straight to the step you need, or tick off Done means at the end.
Git reads several configuration layers. System settings come first, then global files such as ~/.gitconfig and $XDG_CONFIG_HOME/git/config, then the repository's .git/config. A later value normally takes precedence. A write goes to the repository file by default, which is the detail most likely to surprise you.
1. Check the installed command
Confirm the version and read the local synopsis before copying an option from an article written for another release:
$ git --version
git version 2.43.0
$ git config --help
The second command may open a pager or a browser. The installed manual is the authority for the machine where you will run the command. Git's configuration syntax and options evolve, so do not assume that a newer web page describes every older installation.
Checkpoint
You know which Git binary is running and have a repository directory available. Replace /path/to/repository in the next commands with its real path.
2. Set a repository-only identity
Use --local when an identity belongs to one repository, such as a work project or a separate personal address. This writes .git/config and does not alter your global defaults:
$ cd /path/to/repository
$ git config --local user.name 'Your Name'
$ git config --local user.email '[email protected]'
$ git config --local --get-regexp '^user\.(name|email)$'
user.name Your Name
user.email [email protected]
Use an address that you are comfortable publishing in commit metadata. This setting is not an authentication credential and does not change your hosting account. If the command says it is not inside a work tree, check your directory with git rev-parse --show-toplevel rather than adding sudo.
To undo these two writes, remove only the keys you set:
$ git config --local --unset user.name
$ git config --local --unset user.email
An unset command returns a non-zero status if the key is absent. That is useful in scripts, but harmless when you are checking a setting interactively.
3. Choose global scope deliberately
Use --global for a preference that should apply across your repositories. For example, this enables coloured Git output where Git decides it is appropriate:
$ git config --global color.ui auto
$ git config --global --get color.ui
auto
This changes a file in your home directory. It is an ordinary user operation, not an administrative one, but it affects future Git commands outside the current repository. Remove it to return to Git's normal fallback behaviour:
$ git config --global --unset color.ui
Do not use --system as a way around a permission error. It writes the system-wide configuration, normally under Git's installation prefix, and requires suitable administrative access. Change it only when you own the host policy and have a recovery plan. A broken system file can affect every user and repository.
4. Inspect the winning value and its origin
A plain lookup can hide the layer that supplied a value. Ask Git to show the scope and source file:
$ git config --show-scope --show-origin --get user.email
local file:.git/config [email protected]
The exact path and value will differ. In a repository, local means .git/config; global means one of the user configuration files. If several layers define a key, list them all with their origins:
$ git config --show-scope --show-origin --get-all user.email
global file:/home/you/.gitconfig [email protected]
local file:.git/config [email protected]
The last value usually wins for a single-valued option, while multi-valued options retain multiple entries. The output can expose an unexpected system or global setting without editing anything.
For a broad audit, list configuration with the same provenance columns:
$ git config --show-scope --show-origin --list
This may open a pager and may include credentials helper commands or other sensitive-looking paths. Treat the output as private. Do not paste it into an issue or chat without reviewing it.
5. Read values in the type Git expects
Use --type when a script or check needs canonical output. For example, Git accepts several boolean spellings but reports a canonical boolean:
$ git config --local core.filemode
true
$ git config --local --type=bool core.filemode
true
The type must match the option. For example, core.autocrlf accepts values such as true, false and input, so reading it as a boolean is an error. An integer can be read with --type=int; a path can be expanded for reading with --type=path. A type check that fails is a useful signal that the stored value is unsuitable for the operation you are about to perform.
When a missing key is an expected case, provide a default instead of turning a normal lookup into an error:
$ git config --local --default '(not set)' --get user.signingkey
(not set)
6. Treat repeated keys as a list
Some options legitimately occur more than once. Use --add to append an entry and --get-all to inspect every entry:
$ git config --local --add remote.origin.fetch '+refs/tags/*:refs/tags/*'
$ git config --local --get-all remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
+refs/tags/*:refs/tags/*
Do not use a plain git config name value when you intend to append. Its normal write replaces at most one matching line. To remove exactly the entry added above, use a fixed-value match:
$ git config --local --fixed-value --unset remote.origin.fetch '+refs/tags/*:refs/tags/*'
Use --unset-all only when you have checked the complete list and really mean to remove every value for that key. That is a destructive configuration change: copy the relevant output first if the entries are difficult to reconstruct.
7. Diagnose failures without guessing
Git returns status 0 on success. Invalid names, malformed configuration, unwritable files and missing keys for an unset operation produce non-zero statuses. Capture the status immediately when testing a script:
$ git config --local --get does.not.exist
$ printf 'exit status: %s\n' "$?"
exit status: 1
If a lookup reports an invalid configuration file, stop and inspect the named file before making more changes. If a setting appears to be ignored, run the same lookup with --show-origin and --show-scope. Check whether an included file, a more specific repository setting, or a one-command git -c name=value ... override is taking precedence.
Keep edits narrow. git config --edit opens a selected file for manual editing, but a typo can invalidate the whole file. Prefer a single-key command when you know the exact key and value, and make a backup before a broad manual rewrite.
Done means
- You can distinguish local, global and system configuration before writing.
- Your identity or preference is stored in the intended file.
- You verified the effective value with its scope and origin.
- You used
--typeonly when the option's value type matched. - You treated repeated keys as a list and removed only the entry you meant to remove.
- You know how to undo each change and have not used elevated privileges unnecessarily.