Sub topic 7
Changing, unsetting and editing settings
This chapter assumes you have Git installed, can use a terminal, and know what a repository and a commit are. It also assumes you have met the system, global and local scopes and can list settings with git config --list --show-origin. Here you learn the three ways to change a setting: writing it, removing it, and editing the file by hand.
Writing a value
The basic form is git config <scope> <key> <value>. The scope flag decides which file Git writes to. If you leave it out, Git writes to the local scope, and that fails outside a repository.
git config --global user.name "Ada Lovelace"
git config --global user.email "[email protected]"Running the same command again replaces the old value. You don't need to remove a setting before changing it.
To override a value for one repository, run the command inside it with --local. This writes to .git/config.
cd my-project
git config --local user.email "[email protected]"After a change, check the result and its source:
git config --show-origin --get user.emailInside my-project this prints the local file path. Anywhere else it prints the global one. That difference is the scope precedence at work.
Removing a value with --unset
Use --unset to delete a key. Name the same scope you used when you set it. Git removes the key only from that file, so a value from a wider scope can reappear.
git config --global alias.st status
git config --global --unset alias.stThe first line creates an alias, so git st runs git status. The second line removes it.
git config --local --unset user.email
git config --get user.emailAfter the local override is gone, the second command prints the global email again.
Editing the file directly
For several changes at once, open the file in your editor:
git config --global --editGit uses the editor from core.editor, then falls back to the GIT_EDITOR, VISUAL and EDITOR environment variables. Use --local or --system to open the other files. Writing to the system file usually needs administrator rights.
The file uses an INI-like format. Section names go in square brackets, and each setting is an indented key = value line:
[user]
name = Ada Lovelace
email = [email protected]The --edit route skips validation until Git next reads the file. A typo such as a missing bracket makes later Git commands fail with a message like fatal: bad config line 3 in file /home/ada/.gitconfig. Fix the line named in the message and run the command again.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `fatal: not in a git directory` | `--local` or no scope flag used outside a repository | Move into the repository or add `--global` |
| Value still shows after `--unset` | It is set in another scope | Run `git config --list --show-origin` and unset it in the file shown |
| `warning: ... has multiple values` | The key is set more than once in one file | Use `--unset-all` to remove them, or edit the file |
| Change has no effect | You wrote to a different scope than you expected | Check the path printed by `--show-origin` |
Exercise
You set user.email with --global and also with --local in a repository. You run git config --global --unset user.email inside that repository, then git config --get user.email. Which email do you see, and why?
Show answer
The local email. The --unset command only removed the key from the global file. The local value in .git/config still exists and takes precedence inside the repository.