Sub topic 3
First settings: identity, editor and default branch
This chapter assumes you can run Git commands in a terminal and know that git config reads and writes settings, which the previous chapter covered. Three settings are worth configuring on every new machine: who you are, which editor Git opens, and what the first branch of a new repository is called.
Identity: user.name and user.email
Every commit records an author name and email address. Git takes both from the user.name and user.email settings when you run git commit. If they are missing, Git may guess a value from your system username and hostname. It prints a warning when it does. Those guessed values end up in the permanent history.
Set both once in the global file, so they apply to every repository you create or clone:
git config --global user.name "Ada Lovelace"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global alias.st "status -sb"
git config --list --show-originThe last command prints every active setting, each prefixed with the file it came from. After the commands above, you should see lines like file:/home/ada/.gitconfig user.name=Ada Lovelace.
The name does not need to be your legal name. The email matters more. Hosting services such as GitHub and GitLab use it to link commits to your account, so use an address registered there, or the private "noreply" address the service offers.
The editor
Git opens an editor for commit messages, interactive rebases and some merges. It picks the first value it finds in this order:
- The
GIT_EDITORenvironment variable. - The
core.editorsetting. - The
VISUALenvironment variable. - The
EDITORenvironment variable. - A compiled-in default, usually
vi.
If you already export EDITOR in your shell profile, you may not need to set anything. Set core.editor when you want Git to use a different editor from your other tools:
git config --global core.editor "nano"
git config --global core.editor "code --wait"The second line is for Visual Studio Code. The --wait flag makes the command block until you close the file tab. Without it, the editor command returns immediately, Git sees an empty message and aborts the commit. Any graphical editor needs an equivalent flag.
The default branch name
When you run git init, Git creates the first branch. Older versions always called it master. Since Git 2.28 you can choose the name with init.defaultBranch. Setting it to main matches the convention of most hosting services:
git config --global init.defaultBranch main
git init demo
git -C demo branch --show-currentThe last command prints main.
This setting affects only repositories you create with git init afterwards. It does not rename branches in existing repositories, and it does not affect git clone, which uses whatever branch the remote repository declares as its default. To rename the branch in an existing repository, run git branch -m master main, then update the remote and any collaborators separately.
How the three settings fit together
Common problems
- Commits show the wrong email. A repository's local setting overrides the global one. Run
git config --show-origin --get user.emailinside the repository to see which file supplies the value. - The commit aborts with "empty message". The editor command returned before you saved. Add the wait flag your editor needs.
- A new repository still uses
master. Your Git is older than 2.28, or the setting was written to a different scope. Check withgit config --show-origin --get init.defaultBranch. - You want Git to refuse guessed identities. Set
user.useConfigOnlytotrue. Git then errors out in any repository where no identity is configured. This is useful when you use different emails per project.
Exercise
You set user.email globally, but a commit in one repository shows a different address. Which command shows where the value comes from, and why can the repository value win?
Show answer
Run git config --show-origin --get user.email inside the repository. The local scope (.git/config) has higher precedence than the global scope (~/.gitconfig), so a local user.email overrides the global one.
Exercise
After running git config --global init.defaultBranch main, you clone an existing project whose default branch is master. What is the name of the branch you get, and why?
Show answer
You get master. init.defaultBranch only applies to repositories created with git init. A clone uses the default branch that the remote repository declares.