Sub topic 7
Setting your identity and default branch
This chapter assumes you have already installed Git and confirmed it with git --version. You should be comfortable opening a terminal and running commands. By the end you will know why Git asks for a name and email, how to set them, how to choose the default branch name for new repositories, and how to check and fix what you set.
Why Git needs an identity
Every commit records who made it. Git stores two values with each commit: an author name and an author email. It does not check them against any account. Git has no login system. It trusts what is in its configuration.
If you skip this step, Git will try to guess from your username and hostname. On many systems it then refuses to commit, or it commits with an address like [email protected]. Neither outcome is what you want. A history full of guessed identities is hard to read, and fixing it later means rewriting commits.
The identity matters for three practical reasons:
- Teammates use it to see who changed a line (
git log,git blame). - Hosting services such as GitHub and GitLab match the commit email to an account, so your commits show your avatar and count toward your contribution history.
- Every commit's hash depends on the author, the date, and the message. The identity is baked in. Changing it later produces a different commit.
The name does not have to be your legal name. Use whatever name your team expects. The email should be one you are happy to have in a permanent history. If the repository is public, anyone can read it.
Setting name and email
Use git config with the --global flag to set your identity once for your user account:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"Quote the name if it contains spaces. Without quotes, the shell splits it into separate arguments, and Git rejects the command with an error such as error: wrong number of arguments.
Many hosting services give you a private "no-reply" email address so that your real address is not exposed in public commits. GitHub, for example, offers an address of the form [email protected]. Check your account settings for the exact value. If you use one, set it here.
Confirming what you set
Read a single value back with:
git config user.name
git config user.emailList everything in your global configuration with:
git config --global --listThe output is a series of key=value lines, for example:
user.name=Your Name
[email protected]
init.defaultbranch=mainKey names are case-insensitive in the section and variable parts, and Git prints them in lower case. So init.defaultBranch appears as init.defaultbranch. This is normal.
The default branch
A branch is a movable label that points at a commit. When you create a repository, Git creates one branch automatically. The name of that first branch is the default branch.
For many years Git named it master. Many projects and hosting services have moved to main. Recent Git versions let you choose the name that git init uses. Set it like this:
git config --global init.defaultBranch mainThis setting only affects repositories you create after setting it. It does not rename branches in existing repositories.
Why the setting matters
Without init.defaultBranch, the name depends on your Git version. Older versions always use master. Newer versions may print a hint about the choice and still use master. If you later push to a hosting service that expects main, a mismatch causes extra steps: you would have to rename the branch or push a differently named branch.
Setting the value once removes that variation. Every new repository on your machine starts with the same name.
Checking your Git version
The init.defaultBranch setting was added in Git 2.28. Check your version first:
git --versionIf it reports a version older than 2.28, the setting is ignored. You have two options. Upgrade Git, which is the better choice. Or use git init -b main is also unavailable in older versions, so rename the branch after the first commit with git branch -m main.
Overriding the default for one repository
Pass the name when you create a repository:
git init --initial-branch=main project-a
git init -b trunk project-b-b is the short form of --initial-branch. The command-line option takes priority over the configuration value.
Renaming an existing branch
If you already made a repository with master and want main, rename it from inside the repository:
git branch -m master mainIf you had already pushed the old name to a remote, renaming locally does not change the remote. You would also need to push the new name and update the remote's default branch in its settings. Do that in coordination with your team, because other people's clones still track the old name.
Where the values are stored
git config --global writes to a plain text file in your home directory, normally ~/.gitconfig. On newer setups it may instead be ~/.config/git/config. You can open it in any editor. After running the commands above, it looks like this:
[user]
name = Your Name
email = [email protected]
[init]
defaultBranch = mainEach [section] corresponds to the first part of a key, and each line beneath it is the rest of the key. So user.name lives under [user] as name.
Editing this file by hand is fine, but using git config avoids typos in the syntax. Git can also open the file for you:
git config --global --editThis uses your configured editor. If no editor is set, Git falls back to a system default such as vi, which can be confusing if you have not used it. You can set a different editor with git config --global core.editor "nano", for example. A later chapter covers configuration scopes in more detail. For now, remember that --global means "for my user account, across all repositories".
Per-repository identity
Sometimes you need a different identity in one repository. A common case is keeping work and personal projects separate. Run the same command without --global from inside that repository:
cd work-project
git config user.email "[email protected]"Without --global, Git writes to .git/config in that repository. That value overrides the global one for this repository only. Check which value is in effect and where it came from:
git config --show-origin --get user.emailThe output names the file, followed by the value:
file:.git/config [email protected]This command is the quickest way to diagnose "why are my commits using the wrong email?". It shows the winning value and its source.
Verifying with a real commit
The best test is to make a commit and look at the author. In a scratch directory:
mkdir identity-check && cd identity-check
git init
echo "test" > note.txt
git add note.txt
git commit -m "Check identity"
git log -1 --format="%an <%ae>"
git branch --show-currentThe git log line prints the author name and email recorded in the commit. The last line prints the current branch name. You should see your name, your email, and main.
The format codes are %an for author name and %ae for author email. git branch --show-current is available in Git 2.22 and later.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Author identity unknown` and `fatal: unable to auto-detect email address` on commit | `user.name` and `user.email` are not set | Run the two `git config --global` commands |
| New repositories still start on `master` | `init.defaultBranch` not set, or Git older than 2.28 | Set the value, and check `git --version` |
| Commits show a different email than expected | A repository-level value overrides the global one | Run `git config --show-origin --get user.email` |
| `error: wrong number of arguments` | Name with spaces was not quoted | Wrap the name in double quotes |
| Commits do not link to your account on the hosting service | The commit email is not attached to your account | Add the email to your account, or change `user.email` |
| Old commits have the wrong identity | The identity was wrong when you committed | Setting config now does not change old commits |
The last row is worth stressing. Configuration applies at the moment you create a commit. Changing it afterwards does not touch existing commits. To fix the most recent commit you can run git commit --amend --reset-author, which recreates it with your current identity. Fixing a longer history requires rewriting it, and that is a risky operation once the commits are shared.
How the pieces fit
The diagram shows how Git decides which value applies when you ask for a setting. More specific files override more general ones.
This is why a repository-level email can silently beat the global one. The next chapter on configuration scopes covers the system level and the full lookup order.
Summary of the commands
git config --global user.name "Your Name"sets the author name.git config --global user.email "[email protected]"sets the author email.git config --global init.defaultBranch mainsets the first branch name for new repositories.git config --global --listshows your global settings.git config --show-origin --get <key>shows the effective value and its file.
Exercise
You set git config --global user.email "[email protected]", but a commit in one project shows [email protected] as the author. What is the most likely cause, and which command confirms it?
Show answer
The project has a local setting in .git/config that overrides the global value. Run git config --show-origin --get user.email inside the project. It prints the file the winning value came from, which will be .git/config. Remove it with git config --unset user.email to fall back to the global value.
Exercise
You set init.defaultBranch to main and then run git init in a folder that already contains a repository on master. Does the branch name change? How would you rename it?
Show answer
No. The setting only applies to newly created repositories, and running git init in an existing repository does not rename its branches. Rename it from inside the repository with git branch -m master main. If the old name was pushed to a remote, you also have to push the new name and update the remote's default branch.
Exercise
Write the commands that create a repository called notes whose first branch is named trunk, without changing your global configuration. Then write the command that prints the branch name.
Show answer
git init -b notes-placeholder is wrong because -b takes the branch name and the directory is a separate argument. Use git init -b trunk notes, then cd notes and git branch --show-current. The -b option overrides init.defaultBranch for this one repository.