Getting Git, and Telling It Who You Are
Installing Git takes one command; configuring it takes four, and skipping them is why somebody's first hundred commits are authored by unknown@localhost. Set identity, default branch, editor, and line endings before the first commit rather than after.
Installing Git takes one command; configuring it takes four, and skipping them is why somebody's first hundred commits are authored by unknown@localhost. Set identity, default branch, editor, and line endings before the first commit rather than after.
Installing Git is one command on every system
Check first — you may already have it. macOS ships a Git, and most Linux distributions install one by default.
$ git --versionAnything from 2.30 onwards is fine for everything in this track. If the command is not found, pick your system:
# macOS — Homebrew, and the version is newer than Apple's$ brew install git # Windows — the installer also gives you Git Bash, which you want$ winget install --id Git.Git -e # Debian or Ubuntu$ sudo apt update && sudo apt install git # Fedora$ sudo dnf install gitYour name and email are stamped into every commit
Git records an author on every commit, and it takes that from your config. Set it once, per machine, before you do anything else.
$ git config --global user.name "Your Name"$ git config --global user.email "[email protected]"The name is what appears in git log and on every GitHub commit page. Use the name you would want a stranger reading your code to see.
The email is more consequential than it looks: GitHub matches commits to accounts by email address. Commit with an address GitHub does not know about and your commits appear on the site with a grey silhouette instead of your avatar, and they do not count towards your contribution graph. Nothing warns you.
Set the default branch to main, once
Git's historical default branch name was master. Since 2020 the industry standard — and GitHub's default for new repositories — is main. Git itself still defaults to the old name unless you tell it otherwise, which produces a genuinely annoying mismatch on your very first push.
$ git config --global init.defaultBranch mainTwo more settings that are worth the thirty seconds. Git opens an editor when a commit message needs more than one line, and its default on many systems is vim — which is a fine editor and a confusing surprise if you have never met it and cannot work out how to quit.
# Use VS Code, and wait for the window to close$ git config --global core.editor "code --wait" # Or nano, which tells you its own keys at the bottom of the screen$ git config --global core.editor nanoLine endings are the classic cross-platform trap
Windows ends a line of text with two invisible characters, carriage return and line feed. macOS and Linux use one, line feed. Git compares files line by line and treats those invisible characters as part of the line.
So on a mixed team, somebody opens a file, saves it without typing anything, and Git reports every single line as changed. The diff is unreadable and the pull request is useless.
# On Windows$ git config --global core.autocrlf true # On macOS or Linux$ git config --global core.autocrlf inputConvert to line feed when committing, convert back to Windows endings when checking out. The repository stays clean and your editor stays happy.
Convert to line feed when committing, and change nothing on checkout. Correct on systems that already use line feed.
Store whatever is in the file. Fine on a solo project, and the source of the every-line-changed diff on a mixed team.
On a shared project the better answer is a .gitattributes file committed to the repository, which sets this for everybody regardless of what each person configured. One line covers the common case:
* text=autoConfig lives at three levels, and the closest one wins
Every setting can be written in three places, and Git reads them in order with the most specific winning.
- 1
System — /etc/gitconfig
Every user on the machine. Set with --system, usually by whoever installed Git. You will rarely touch it.
- 2
Global — ~/.gitconfig
Your user account, across every repository. Set with --global. This is where the four settings above belong.
- 3
Local — .git/config in the repository
This project only. No flag needed. This is how you commit to a work project with a work email and a personal one with a personal email.
When something behaves oddly and you cannot see why, ask Git where a setting came from. The answer includes the file, which is usually the whole explanation.
# Everything, with the file each value came from$ git config --list --show-origin # One setting, and where it was defined$ git config --show-origin user.email # Override it for this repository only$ git config user.email "[email protected]"Before your first commit
- ✓git config --global user.name — the name that goes on every commit forever
- ✓git config --global user.email — matching a verified address on your GitHub account, or its no-reply form
- ✓git config --global init.defaultBranch main — so your local branch name matches GitHub's
- ✓git config --global core.editor — anything you know how to quit
- ✓git config --global core.autocrlf — true on Windows, input everywhere else
Key takeaways
- Check for git --version first; macOS and most Linux systems already have one.
- On Windows, install Git for Windows and use Git Bash, so the commands in every tutorial work verbatim.
- user.name and user.email are stamped into every commit and cannot be changed later without rewriting history.
- GitHub links commits to accounts by email address. A mismatch means grey avatars and no contribution graph, with no warning.
- GitHub's no-reply address keeps your real one out of a permanent public record while still linking commits to you.
- init.defaultBranch main aligns your local default with GitHub's, avoiding a first-push mismatch.
- Set core.editor to something you can exit. The vim default has trapped a generation.
- core.autocrlf stops the every-line-changed diff on mixed-platform teams; .gitattributes fixes it for the whole team at once.
- Config reads system, then global, then local, and the closest one wins. --show-origin tells you which file to blame.
Quick check
Answer these to unlock the next chapter — 3 of 4 to pass. You can retake it anytime.
Answer every question to check.
Make a free account to read on
Every chapter is free — an account is how your progress, XP, and streak follow you from your laptop to your phone, and how you show up on the leaderboard. No payment, no trial.