Sub topic 3
Setting your name and email
Every commit records who made it. Git reads that identity from two settings, user.name and user.email. This chapter shows how to set them, check them, and override them for one repository.
This chapter assumes you have Git installed, can use a terminal, and know what a commit is. It also assumes you know the three configuration scopes (system, global, local) from the previous chapter.
Why identity matters
Git stores the author name and email inside each commit. They become part of the commit's history and appear in git log. Hosting services such as GitHub also use the email to link commits to your account.
Git does not verify this identity. It is plain text that you choose, not a login. Proving who you are is a separate job, handled by credentials and signing.
If no identity is set, Git may try to build one from your system account and hostname. If it can't, git commit stops with "Author identity unknown". Set the identity before your first commit so you avoid both outcomes.
Set a global identity
Use --global to store the values in your user-level configuration file. They then apply to every repository on your account.
git config --global user.name "Ada Lovelace"
git config --global user.email "[email protected]"Quote the name if it contains spaces. Use an email address that your hosting service recognizes. Many services offer a private "no-reply" address if you don't want to publish your real one.
Check what Git will use
Reading a value back confirms it. Adding --show-origin also shows which file supplies it.
git config --show-origin --get user.emailThe output has the file path first, then the value:
file:/home/ada/.gitconfig [email protected]When a value looks wrong, the file path usually tells you why. A repository's .git/config may be overriding your global value.
Override the identity for one repository
Use --local to write to the current repository's .git/config. Run it from inside the repository.
cd my-project
git config --local user.email "[email protected]"Commits in my-project now use the work email. Every other repository still uses the global one. The name is not overridden here, so it still comes from the global file.
The next chapters cover the full order of scopes. For identity, remember that the narrower scope wins.
Common mistakes
- Wrong email on old commits. Changing the configuration affects only future commits. To fix the most recent commit, run
git commit --amend --reset-author --no-edit. Rewriting older commits is a bigger operation and needs care if you have already shared them. - Local override you forgot about. A repository that still shows the wrong email usually has a
user.emailin its own.git/config. Rungit config --show-origin --get user.emailinside it to find out. - Unquoted names.
git config --global user.name Ada Lovelacefails because Git sees an extra argument. Always quote names that contain spaces.
Exercise
You set a global email of [email protected]. Inside the repository work-app, commits still show [email protected]. Which command shows where that value comes from, and what is the likely cause?
Show answer
Run git config --show-origin --get user.email inside work-app. The output will most likely point to work-app/.git/config, meaning a local user.email overrides the global one. Remove it with git config --local --unset user.email or change it with git config --local user.email "[email protected]".