Sub topic 7
Setting your identity and default branch
This chapter assumes you have installed Git and confirmed it with git --version. You should be able to open a terminal and run simple commands. By the end, you will have set the name, email, and default branch name that Git uses, and you will know how to check them.
Why Git asks who you are
Every commit records an author. Git does not log in to anything and does not verify who you are. It reads two values from its configuration, user.name and user.email, and stamps them on each commit you make.
If these values are missing, Git may refuse to commit. Sometimes it guesses a name and address from your system account. Those guesses often look like [email protected], and they end up permanently in the history. Setting the values once, before your first commit, avoids that.
Setting your name and email
Use git config with the --global flag. The flag stores the value for your user account, so it applies to every repository you create.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"Quote the name if it contains spaces. Use an email address you are comfortable having in public history. If you plan to push to a hosting service such as GitHub, check whether it offers a private "noreply" address. Some services match commits to your account by email, so the address should be one the service recognizes.
Setting the default branch name
A branch is a movable pointer to a line of commits. When you run git init, Git creates one branch for you. Older Git versions named it master by default. Many hosting services and projects now use main, and you can tell Git to do the same:
git config --global init.defaultBranch mainThis setting only affects repositories you create after setting it. Existing repositories keep their current branch name.
Reviewing what you set
List all global settings with:
git config --global --listExpect output similar to this:
user.name=Your Name
[email protected]
init.defaultbranch=mainGit prints key names in lowercase, so init.defaultbranch is the same setting you typed as init.defaultBranch. To read a single value, name the key:
git config user.emailThe sequence in one picture
Where the values are stored
git config --global writes to a plain text file in your home directory. This is usually ~/.gitconfig, and Git also reads ~/.config/git/config if it exists. You can open the file in any editor and change it by hand. The next chapter covers the three configuration scopes in more detail. For now, remember that --global means "for my user account".
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Commit fails with "Please tell me who you are" | `user.name` or `user.email` is not set | Run the two `git config --global` commands above |
| New repository still uses `master` | `init.defaultBranch` was not set, or the repository was created before you set it | Set the option, then create a new repository. To rename an existing branch, use `git branch -m main` |
| Commits show a wrong or old email | A repository-level value overrides the global one | Run `git config user.email` inside that repository to see the effective value |
| Name is cut off after the first word | The name was not quoted | Repeat the command with the full name in double quotes |
Checking that it works
A quick test confirms the whole chain. Create a scratch repository, make one commit, and inspect the author and branch name.
mkdir identity-test && cd identity-test
git init
echo "test" > note.txt
git add note.txt
git commit -m "Test 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 should print main. If either result is wrong, review your settings with git config --global --list.
Exercise
You set init.defaultBranch to main today, but a repository you created last week still has a branch called master. Why, and how can you rename that branch?
Show answer
The setting is read only when git init creates a repository, so it does not change existing repositories. Inside the old repository, run git branch -m main to rename the current branch.
Exercise
Inside one repository, git config --global user.email prints [email protected], but new commits show [email protected]. What is the most likely explanation, and what command shows the value Git actually uses?
Show answer
A repository-level setting for user.email overrides the global one. Running git config user.email inside the repository, without --global, prints the effective value.