Sub topic 4
Telling Git who you are
Every commit records who made it. This chapter shows how to set that identity, where Git stores it, and how to fix the error you get when it is missing. It assumes you can use a terminal, have Git installed, and have created a repository with git init.
Why Git needs your name and email
A commit stores an author name and email next to the snapshot and the message. Later, git log shows these fields, so teammates know who to ask about a change. Git does not check them. They are labels, not logins, and they have nothing to do with the password or SSH key you use to reach a remote server.
Setting your identity
Set both values once with git config. The --global flag applies them to every repository for your user account.
git config --global user.name "Ada Lovelace"
git config --global user.email "[email protected]"
git config --global --listThe last command prints every global setting. You should see user.name=Ada Lovelace and [email protected] in the output. To read a single value, use git config user.email.
Where the settings live
Git reads configuration from three levels. When the same key appears at more than one level, the most specific level wins.
| Level | Flag | Typical file | Applies to |
|---|---|---|---|
| System | `--system` | `/etc/gitconfig` | Every user on the machine |
| Global | `--global` | `~/.gitconfig` | Your user account |
| Local | `--local` (default inside a repository) | `.git/config` | One repository |
This lets you keep a personal address as the global default and override it in a work repository. Run the command from inside that repository and omit --global:
git config user.email "[email protected]"
git config --show-origin user.emailThe second command prints the value and the file it came from, which is the fastest way to find out why Git is using an unexpected address.
Common errors
- Author identity unknown. If no name or email is set,
git commitstops and asks you to rungit config. Set the values and repeat the commit. Some systems skip the error and build an identity from your username and hostname instead. That produces an address like[email protected], which is easy to miss. - Wrong email in a work repository. A global personal address ends up in work commits. Set a local
user.emailin that repository. - Expecting old commits to change. Changing the configuration affects only future commits. Existing commits keep the identity they were created with.
- Quoting. Wrap a name that contains spaces in quotes. Without quotes, Git treats the extra words as unexpected arguments.
Exercise
You set user.email globally to a personal address. In one repository you run git config user.email "[email protected]" without --global. You then commit in that repository. Which address is recorded, and which command shows where Git got it?
Show answer
The commit records [email protected], because the local setting in .git/config overrides the global one. git config --show-origin user.email prints the value together with the file it was read from.