Configuring Git
Every commit records who made it, so the first thing to do on a new computer is tell Git your name and email address. A few more settings — the default branch name, your editor and how line endings are handled — make Git pleasant to use from day one.
Your identity
git config --global user.name "Sam Rivera" git config --global user.email "sam@example.com"
These commands print nothing when they succeed. The name can be anything you like — it's shown in the history next to your commits. The email should be the one you'll use on GitHub (or another host), so your commits are linked to your account there.
Read a setting back by leaving out the value:
git config user.name
Sam Rivera
user.email instead of your real one.The default branch name
A new repository starts with one branch. Git has historically called it master; most hosting services and teams now use main. Set it once so every new repository matches:
git config --global init.defaultBranch main
Your editor
Some commands open a text editor — for example when you commit without a message, or during an interactive rebase. If you haven't chosen one, Git falls back to Vim on most systems, which confuses many newcomers. (If you ever get stuck in Vim: press Esc, type :wq and press Enter to save and quit, or :q! to quit without saving.)
# Visual Studio Code ("--wait" makes Git wait until you close the tab) git config --global core.editor "code --wait" # Nano, a simple terminal editor git config --global core.editor "nano" # Notepad on Windows git config --global core.editor "notepad"
Three levels of configuration
| Option | Applies to | Stored in |
|---|---|---|
--system | Every user on the computer | A file in Git's install folder (e.g. /etc/gitconfig) |
--global | All your repositories | ~/.gitconfig in your home folder |
--local (the default inside a repo) | One repository | .git/config inside that repo |
More specific levels win: a local setting overrides the global one. That's handy when you use a work email for one project:
mkdir work-project
cd work-project
git init --quiet
git config user.email "sam.rivera@company.example"
git config user.email
sam.rivera@company.example
cd .. git config user.email
sam@example.com
Inside work-project Git uses the work address; everywhere else it uses the global one.
Listing your settings
git config --global --list
user.name=Sam Rivera user.email=sam@example.com init.defaultbranch=main
To see which file a value came from, add --show-origin:
git config --show-origin user.name
file:/home/sam/.gitconfig Sam Rivera
The global file is plain text, and you can edit it directly:
cat ~/.gitconfig
[user] name = Sam Rivera email = sam@example.com [init] defaultBranch = main
Line endings
Windows ends lines of text with two characters (carriage return + line feed), while macOS and Linux use just a line feed. When people on different systems share files, this can make every line look changed. The usual recommendation:
# On Windows: convert to LF when committing, back to CRLF when checking out git config --global core.autocrlf true # On macOS and Linux: convert any CRLF to LF when committing, never the other way git config --global core.autocrlf input
Lesson 43 shows a more robust approach that works for the whole team: a .gitattributes file committed to the repository.
Removing a setting
git config --global --unset core.editor git config --global --list
user.name=Sam Rivera user.email=sam@example.com init.defaultbranch=main
Getting help
Every Git command has a manual page:
git help config # full manual, opens in a pager or browser git config -h # short summary of the options in the terminal
Try it yourself
Set your own user.name, user.email, init.defaultBranch and an editor you're comfortable with, then run git config --global --list to check all four.