Installing and configuring Git

Topic 3 of 3 in Getting started7 min readBeginnerFree course

Overview

Before your first commit, you need Git installed and a few settings in place — most importantly your name and email, because Git writes them into every commit. This takes five minutes and you’ll only do it once per computer.

Installing Git

Windows

Download Git for Windows from git-scm.com. The installer asks many questions; the defaults are good. Two worth knowing:

  • Default editor: choose one you know (VS Code, Notepad++ or Nano) rather than the default Vim if you’ve never used Vim.
  • Line endings: “Checkout Windows-style, commit Unix-style line endings” (the default) is right for most people.

It installs Git Bash, a terminal that understands the same commands as macOS and Linux. Use it for this course — every example will work as written. Alternatively, winget install --id Git.Git -e installs it from the command line.

macOS

Open Terminal and run git --version. If Git isn’t installed, macOS offers to install the Xcode Command Line Tools, which include it. For a more up-to-date version, use Homebrew: brew install git.

Linux

Use your distribution’s package manager:

Terminal
sudo apt install git        # Debian, Ubuntu
sudo dnf install git        # Fedora
sudo pacman -S git          # Arch

Long-term-support distributions sometimes ship an older Git. Everything in this course works on Git 2.30 or newer; if yours is older, your distribution usually has a way to get a newer one (Ubuntu’s git-core PPA, for example).

Check it worked

Terminal
git --version
# git version 2.56.1

Telling Git who you are

Every commit records an author name and email. Set them globally — for every repository on this computer:

Terminal
git config --global user.name "Asha Rao"
git config --global user.email "asha@example.com"

Use the email address you’ll use for your GitHub account, so GitHub can link your commits to your profile. If you’d rather keep your address private, GitHub gives you a noreply address (in Settings → Emails) that you can use here instead.

If you skip this step, Git refuses to commit and tells you to set them — or guesses from your computer’s username, which looks unprofessional in a shared history.

Terminal
# New repositories start on a branch called main (instead of master)
git config --global init.defaultBranch main

# Your editor, used for commit messages and interactive commands
git config --global core.editor "code --wait"   # VS Code
# git config --global core.editor "nano"        # or a simple terminal editor

# Coloured output (on by default in modern Git, but harmless to set)
git config --global color.ui auto

# The first git push of a new branch links it to the same-named branch on GitHub
git config --global push.autoSetupRemote true

A few notes:

  • init.defaultBranch main: GitHub, GitLab and most teams use main. Git 3.0 will make main the default too; until then, this setting avoids a confusing hint every time you run git init.
  • core.editor: the --wait flag matters for graphical editors — it makes Git wait until you close the file before continuing.
  • push.autoSetupRemote (Git 2.37+): without it, the first push of every new branch fails with a message asking you to run git push --set-upstream origin <branch>. With it, a plain git push just works. You’ll see why in Pushing and pulling.

You’ll also meet a setting for git pull later: when your branch and GitHub’s have both moved on, modern Git stops and asks whether to merge or rebase. Pushing and pulling explains the choice, so leave it until then.

Where settings live

Where Git settings live. –system: every user on this computer, stored in /etc/gitconfig. –global: you, in every repository, stored in ~/.gitconfig. –local: this repository only, the default, stored in .git/config. Later levels win. Example commands: git config –global user.name “Asha Rao”, and git config –list –show-origin.

Git reads settings from three levels. When the same setting appears at more than one, the most specific wins: local beats global, which beats system.

This lets you have a personal email globally but a work email in work repositories:

Terminal
cd ~/work/billing-service
git config user.email "asha.rao@company.example"   # local: this repository only

To see what’s in effect and where each value came from:

Terminal
git config --list --show-origin
git config user.email              # the value that applies here
git config --global --edit         # open your global config file in your editor

The global file is plain text, usually ~/.gitconfig:

INI
[user]
	name = Asha Rao
	email = asha@example.com
[init]
	defaultBranch = main
[push]
	autoSetupRemote = true

Line endings (Windows users, read this)

Windows ends lines with two characters (CRLF); macOS and Linux use one (LF). If team members mix them, every line of a file can appear changed. Git for Windows sets core.autocrlf true by default, which converts automatically. For team projects, a .gitattributes file in the repository is even better, because it applies to everyone regardless of their settings:

Text
* text=auto

Getting help

Terminal
git help config          # the full manual page for a command
git config -h            # a short summary of options

The manual pages are also online at git-scm.com/docs. The free book Pro Git on the same site is an excellent reference.

Check: run git config user.name and git config user.email. If both print what you expect, you’re ready for your first repository.

Exercise

Configure Git like a professional

Set up your global Git configuration: your name and email, main as the default branch for new repositories, a sensible editor, colour output, and a first git push of a new branch that sets up tracking automatically. Then add a repository-specific override: inside a new work-project repository, use a different email for that project only. Finish by listing your settings with the file each one comes from.

Terminal
# 1. Who you are (use your own name and email)

# 2. Default branch for new repositories

# 3. Editor and colour

# 4. Let the first push of a new branch set up tracking automatically

# 5. A project that uses your work email
mkdir work-project && cd work-project
git init

# 6. Show every setting and where it comes from

Summary

  • Install Git from git-scm.com (Windows), Xcode Command Line Tools or Homebrew (macOS), or your package manager (Linux).
  • Set user.name and user.email before your first commit — they are written into every commit you make.
  • git config --global init.defaultBranch main makes new repositories start on main.
  • Settings live at three levels — system, global (you) and local (one repository) — and the most specific wins.
  • git config --list --show-origin shows every setting and the file it came from.