Sub topic 1
What git config does and why it comes first
Prerequisites
This chapter assumes you have the following:
- Git installed on your machine. Running
git --versionprints a version number. - Basic comfort with a terminal or shell.
- Familiarity with what a Git repository and a commit are.
What git config does
git config reads and writes Git's settings. Every setting is a key and a value. The key has a section and a name, joined by a dot. For example, user.name is the name setting in the user section.
Git consults these settings during almost every command. They decide who is recorded as the author of a commit, which editor opens for a commit message, what a new repository's first branch is called, and how git pull combines histories.
Why it comes first
Git writes the author name and email into every commit. The commit's ID is a hash computed from its contents, and the author is part of those contents. If you commit with the wrong identity, you can't fix it by editing a field. You have to rewrite the commit, which gives it a new ID. That is awkward on a branch you have already shared.
If no identity is set, Git may guess one from your username and hostname, or refuse to commit. The guess is rarely an address you want in project history. Setting your identity before your first commit avoids both outcomes.
The same reasoning applies to other first-run choices. A default branch name, an editor you can actually use, and an explicit pull behavior are all cheaper to set once than to correct after the fact.
The shape of a configuration
Settings live in three scopes, each stored in its own file. Git reads them in order, and a more specific scope overrides a more general one.
- System applies to every user on the machine.
- Global applies to your user account across all repositories.
- Local applies to a single repository.
The next chapters cover each scope in detail. For now, you only need to know that the scope is chosen with a flag such as --system, --global or --local.
A first example
The most important first step is your identity. This writes it at the global scope, so it applies to every repository you create:
git config --global user.name "Ada Lovelace"
git config --global user.email "[email protected]"Read a value back by giving only the key:
git config user.nameThis prints Ada Lovelace. To see every setting Git currently uses, and which file each came from, run:
git config --list --show-originEach output line starts with the file path, followed by a key and value. This is the main tool for answering "why does Git behave this way?" later in the tutorial.
What this tutorial sets up
Over the following chapters you will configure these settings, roughly in order of importance:
| Setting | What it controls |
|---|---|
| `user.name`, `user.email` | Author identity on every commit |
| `init.defaultBranch` | Name of the first branch in new repositories |
| `core.editor` | Editor used for commit messages |
| `core.autocrlf` | Line-ending conversion between operating systems |
| `pull.rebase` | Whether `git pull` merges or rebases |
You will also learn to inspect and change settings, and to apply different identities to different directories.
Exercise
You commit with a misspelled author name. Why is this harder to fix than a typo in a file, and what should you have done beforehand?
Show answer
The author is part of the data used to compute the commit's hash, so changing it means rewriting the commit and producing a new ID. That is disruptive if the commit is already shared. Before the first commit, you should have set user.name and user.email with git config --global, and checked them with git config user.name.
Exercise
Which command shows every setting Git is using along with the file each one comes from?
Show answer
git config --list --show-origin