2. Installation & Configuration

Before you can use Git, you need to install it on your computer and set up your personal configuration options. In this chapter, we will walk through the installation process for Windows, macOS, and Linux, followed by essential setup commands.

Installing Git

1. Installing on Windows

The easiest way to install Git on Windows is to use the official executable installer:

  1. Go to the official Git download page: git-scm.com/download/win.
  2. Download the 64-bit Git for Windows Setup installer.
  3. Run the downloaded `.exe` file and follow the setup wizard. We recommend keeping the default options.
  4. Ensure you check the option to use "Git Bash" (a powerful bash emulator for Windows command lines).

2. Installing on macOS

There are multiple ways to install Git on macOS:

  • Via Xcode Command Line Tools: Open your terminal and run git --version. If not installed, a prompt will ask you to install the command line developer tools.
  • Via Homebrew: If you use Homebrew, open the terminal and run:
    brew install git

3. Installing on Linux

Use your package manager to install Git on Linux distributions:

  • Ubuntu/Debian:
    sudo apt-get update sudo apt-get install git
  • Fedora/Red Hat:
    sudo dnf install git

Verifying Installation

To verify that Git has installed successfully, open your Terminal (Mac/Linux) or Command Prompt/Git Bash (Windows) and type:

git --version

This should print the installed Git version, e.g., git version 2.40.1.

First-Time Git Configuration

Now that Git is installed, you must customize your Git environment. Git provides a tool called git configthat lets you read and set configuration variables that control how Git looks and operates.

Setting Your Identity

The very first thing you should do is set your username and email address. This is critical because every Git commit uses this information, and it is permanently baked into the history:

# Set global username
git config --global user.name "Your Name"

# Set global email address
git config --global user.email "your.email@example.com"

Configuring Default Editor

By default, Git uses your system's default editor (often Vi or Vim) for typing commit messages. If you prefer to use VS Code, you can configure it as follows:

git config --global core.editor "code --wait"

Configuring Default Branch Name

Historically, Git used master as the default branch name when initializing a new repository. In modern versions, you can easily set your default branch to main globally:

git config --global init.defaultBranch main

Checking Your Configurations

To check all configuration settings Git can find in your environment, use:

git config --list

To check a specific value, run git config <key>, for example:

git config user.name

Config Levels Explained

Git configuration variables can be stored in three different levels:

Config LevelFlagDescription
System--systemApplies to all users on this system. Stored in /etc/gitconfig.
Global--globalApplies to your user account specifically. Stored in ~/.gitconfig.
Local--localSpecific to a single repository. Stored in .git/config inside the project.
Note: Local configuration takes precedence over global configuration, which takes precedence over system configuration.