Install and configure Git

Git · Essentials · 8 min

Every commit is stamped with your identity, and a few defaults save pain later.

Install Git from your package manager (apt install git, brew install git, winget install Git.Git) or from git-scm.com, then check it with git --version.

Before your first commit, tell Git who you are. Name and email are written into every commit you make, so use the email your hosting account knows about:

git config --global user.name "Ada Lovelace"
git config --global user.email "ada@example.com"

git config works at three levels, each overriding the one before:

  • --system: every user on the machine.
  • --global: you, stored in ~/.gitconfig.
  • --local (the default inside a repo): one repository, stored in .git/config.

So you can keep a personal email globally and override it with a work email inside work repositories. git config --list --show-origin shows every value and which file it came from, which is the quickest way to debug a surprising setting.

A few settings worth setting on day one:

  • init.defaultBranch main so new repositories start on main.
  • core.editor to an editor you can use ("code --wait", nano, vim), because Git opens it for commit messages.
  • pull.rebase false (or true) so git pull knows how to combine diverged histories instead of asking every time.

On Windows, core.autocrlf true converts line endings for you; on macOS/Linux leave it unset or input.

Example

git --version                         # git version 2.51.0
git config --global user.name "Ada Lovelace"
git config --global user.email "ada@example.com"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
git config --global pull.rebase false

git config user.email                 # read one value
git config --list --show-origin       # every setting and its file

Remember

  • Set user.name and user.email before your first commit
  • Config levels: system < global (~/.gitconfig) < local (.git/config)
  • git config --list --show-origin explains where a value comes from

Watch out for

  • Committing with a wrong email — the commits won't be linked to your hosting account and fixing them later means rewriting history.

Check yourself

  1. You set user.email globally, then a different user.email with --local inside repo A. Which email do commits in repo A use?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup

Builds on

Nothing: this is a foundation.

Unlocks