Installing Git

TL;DR: Installing Git adds the Git command-line engine to your operating system’s executable path, allowing you to run version control operations locally. In addition to the CLI binary, installing Git configures the template files (such as sample hooks) that populate the .git/ folder of every repository you initialize. The installation process varies by operating system, ranging from simple package managers on macOS and Linux to full installer wizards and bash environments on Windows.


What Does Installing Git Actually Do?

When you download and install Git, you are not installing a desktop application with a graphical user interface (unless you explicitly download a GUI client). Instead, you are installing the Git Core Engine, which consists of a set of command-line binaries and support files.

At the OS level, installing Git does three main things:

  1. Places Executables in the System Path: The installer copies the primary git executable (along with low-level helper tools like git-upload-pack and git-receive-pack) into a system folder (such as /usr/bin/ on Unix or C:\Program Files\Git\cmd\ on Windows). It then appends this directory to your system’s PATH environment variable so your terminal can recognize the git command.
  2. Establishes Global Templates: Git copies default files, settings, and sample scripts into a system-wide shared directory (e.g., /usr/share/git-core/templates/ on Linux). When you run git-init in a directory, Git copies these template files to create the skeleton of your new .git/ directory.
  3. Prepares Configuration Files: The installer creates or prepares global configuration levels that allow Git to read settings across all projects on your machine.

What Happens Inside .git/ (Hands-on exploration)

To understand the connection between your system installation and your repositories, let’s explore where Git copies its initial .git/ contents from when you initialize a project.

Step 1: Locate the Git Binary and Templates

First, open your terminal and locate the path of the active git executable on your system:

# On macOS and Linux
$ which git
/usr/bin/git

# On Windows (Git Bash or Command Prompt)
$ where git
C:\Program Files\Git\cmd\git.exe

This tells us where the core command runner is located. Now, let’s locate the system directory containing the default templates that Git installs on your machine.

  • On standard Linux installations (like Ubuntu), this is usually /usr/share/git-core/templates/.
  • On macOS (installed via Homebrew), it can be found under /opt/homebrew/share/git-core/templates/.
  • On Windows, it is located in C:\Program Files\Git\mingw64\share\git-core\templates\.

Let’s list the contents of the system-wide template directory:

$ ls -F1 /usr/share/git-core/templates/
description
hooks/
info/

Does this look familiar? It matches the structure of the .git/ directory! Let’s inspect the system’s /usr/share/git-core/templates/hooks/ directory:

$ ls -l /usr/share/git-core/templates/hooks/
total 40
-rwxr-xr-x 1 root root  478 Jul  7 10:24 applypatch-msg.sample
-rwxr-xr-x 1 root root  896 Jul  7 10:24 commit-msg.sample
-rwxr-xr-x 1 root root 4655 Jul  7 10:24 fsmonitor-watchman.sample
-rwxr-xr-x 1 root root  189 Jul  7 10:24 post-update.sample
-rwxr-xr-x 1 root root  424 Jul  7 10:24 pre-applypatch.sample
-rwxr-xr-x 1 root root 1643 Jul  7 10:24 pre-commit.sample
-rwxr-xr-x 1 root root  416 Jul  7 10:24 pre-merge-commit.sample
-rwxr-xr-x 1 root root 1492 Jul  7 10:24 pre-push.sample
-rwxr-xr-x 1 root root 4898 Jul  7 10:24 pre-rebase.sample
-rwxr-xr-x 1 root root  544 Jul  7 10:24 pre-receive.sample
-rwxr-xr-x 1 root root 1492 Jul  7 10:24 prepare-commit-msg.sample
-rwxr-xr-x 1 root root 3610 Jul  7 10:24 update.sample

These are template shell scripts.

Step 2: Initialize a Repo and Track the Copy Process

Let’s initialize a new repository and watch Git copy these templates:

$ mkdir install-test && cd install-test
$ git init
Initialized empty Git repository in /home/admin/Code/install-test/.git/

If we look inside our newly created .git/hooks/ directory, we see these exact sample files:

$ ls -F1 .git/hooks/
applypatch-msg.sample
commit-msg.sample
fsmonitor-watchman.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
pre-merge-commit.sample
pre-push.sample
pre-rebase.sample
pre-receive.sample
prepare-commit-msg.sample
update.sample

Whenever you run git init, Git copies all files from the template directory installed on your operating system into the local .git/ folder. This ensures that every new repository starts with a standardized set of sample hooks, default files, and ignore properties.


Operating System Installation Guides

Below are the detailed, step-by-step methods to install Git on Windows, macOS, and Linux.

1. Installing on macOS

Mac users have two main paths to install Git: using Apple’s Xcode Command Line Tools or installing via the Homebrew package manager.

Option A: Apple Xcode Command Line Tools (Simplest)

macOS comes with a built-in stub for Git. If you run a Git command in a fresh installation, macOS will display a prompt to install Xcode Command Line Tools.

  1. Open your Terminal (located in Applications/Utilities/Terminal.app).
  2. Run the version check:
    $ git --version
  3. If Git is not installed, a system alert will appear: “The git command requires the Xcode command line developer tools. Would you like to install the tools now?”
  4. Click Install and accept the license terms. macOS will automatically download and install Git.

To control your Git updates and access the latest stable releases, use the Homebrew package manager.

  1. Install Homebrew (if not already installed) by running the official installer script:
    $ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Run the brew installation command:
    $ brew install git
  3. Restart your terminal to refresh the environment variables, then verify:
    $ git --version
    git version 2.45.2  # (or latest version)

2. Installing on Windows

On Windows, Git requires a compatibility layer to run POSIX commands. The standard distribution is Git for Windows (also known as msysGit), which bundles the Git core with a bash emulator called Git Bash.

Step-by-Step Installation Wizard:

  1. Go to the official Git download page and download the 64-bit Git for Windows Setup installer.
  2. Run the downloaded .exe file.
  3. Keep the default path and components. Ensure Git LFS (Large File Support) and Git Integration with Windows Explorer (which adds the right-click “Git Bash Here” shortcut) are selected.
  4. Select default editor: The installer defaults to Vim. If you are not familiar with Vim, select Visual Studio Code or Notepad++ from the dropdown menu.
  5. Adjusting name of initial branch: Choose “Override the default branch name for new repositories” and set it to main (to match modern workflow standards).
  6. Adjusting PATH environment: Select “Git from the command line and also from 3rd-party software”. This allows you to run Git commands from Command Prompt, PowerShell, Git Bash, or third-party IDEs like VS Code.
  7. Choosing HTTPS transport backend: Keep the default “OpenSSL library”.
  8. Configuring the line ending conversions: Select “Checkout Windows-style, commit Unix-style line endings” (core.autocrlf true). This converts LF line endings from repositories into CRLF on your local disk, and converts CRLF back to LF when staging.
  9. Configure terminal emulator: Choose “Use MinTTY” (the default terminal emulator for Git Bash, which provides resizable windows and better terminal fonts).
  10. Click Install. Once finished, open Git Bash from your Start Menu to begin running Git commands.

3. Installing on Linux

Linux distributions provide pre-built Git binaries through their native package managers.

For Ubuntu / Debian-based systems (apt):

$ sudo apt update
$ sudo apt install git -y

For Fedora / RHEL-based systems (dnf):

$ sudo dnf install git -y

For Arch Linux (pacman):

$ sudo pacman -S git --noconfirm

Advanced: Compiling Git from Source on Linux

For systems requiring custom build options or strict version compliance, compiling Git from source is the preferred method.

  1. Install Build Dependencies: On Ubuntu/Debian:
    $ sudo apt update
    $ sudo apt install dh-autoreconf libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev asciidoc xmlto docbook2x make binutils -y
  2. Download the Source Tarball: Go to the Git kernel repository and select the version tarball:
    $ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.45.2.tar.gz
    $ tar -zxf git-2.45.2.tar.gz
    $ cd git-2.45.2
  3. Configure, Compile, and Install:
    $ make configure
    $ ./configure --prefix=/usr/local
    $ make all doc info
    $ sudo make install install-doc install-html install-info
  4. Verify the installation:
    $ git --version
    git version 2.45.2

First-Time Configuration Files

Once Git is installed, you must configure your developer identity. Every Git commit attaches the author’s name and email to the commit hash, which cannot be changed later without rewriting history.

Run the following commands in your terminal to set your identity globally (refer to /git-config for details):

$ git config --global user.name "Alex Developer"
$ git config --global user.email "alex@dotgit.dev"

Let’s inspect where Git writes these settings. In macOS and Linux, global settings are stored in a text file in your home directory:

$ cat ~/.gitconfig
[user]
	name = Alex Developer
	email = alex@dotgit.dev

On Windows, this configuration is written to C:\Users\YourUsername\.gitconfig.


Common Pitfalls and Mistakes

1. The Xcode Command Line Tool Error (macOS)

The Problem: After a macOS system update, running any Git command fails with the error: xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /usr/bin/xcrun The Fix: This happens because macOS updates can remove or deselect the command-line developer tools. Reinstall them by running:

$ xcode-select --install

2. Line Ending Conflicts (CRLF vs. LF)

The Problem: Developing on Windows and committing files leads to warnings like: warning: LF will be replaced by CRLF in index.html. The file will have its original line endings in your working directory. The Fix: Windows uses Carriage Return + Line Feed (\r\n or CRLF) to mark new lines, while macOS and Linux use Line Feed (\n or LF). If not configured correctly, Git will mark files as modified when no visible code has changed. Ensure your line endings are configured correctly for your OS:

# On Windows (converts LF to CRLF on checkout, CRLF to LF on commit)
$ git config --global core.autocrlf true

# On macOS and Linux (preserves LF on checkout and commit)
$ git config --global core.autocrlf input

3. Shell PATH Issues on Windows

The Problem: Running git in Windows Command Prompt or PowerShell returns: 'git' is not recognized as an internal or external command, operable program or batch file. The Fix: The Git installation folder was not added to your system’s PATH environment variables. Re-run the installer and ensure “Git from the command line and also from 3rd-party software” is checked. Alternatively, add C:\Program Files\Git\cmd\ to your user PATH environment variable manually.


Command Quick Reference

Action / PlatformCommand / SourceVerification
Check Installationgit --versionOutputs Git release version
macOS (Homebrew)brew install gitwhich git should output /opt/homebrew/bin/git
Ubuntu/Debiansudo apt install gitwhich git should output /usr/bin/git
Fedorasudo dnf install gitwhich git should output /usr/bin/git
Set Usernamegit config --global user.name "Name"Inspect with git config user.name
Set Emailgit config --global user.email "email"Inspect with git config user.email
Locate Templatesls /usr/share/git-core/templates/Verify default hooks exist

FAQ (People Also Ask)

Why does the terminal say “command not found: git” after installation?

This error means your terminal cannot locate the Git executable file in the directories listed in your system’s PATH variable. To resolve this, restart your terminal to apply the updated environment variables. If you are on Windows, verify that the path C:\Program Files\Git\cmd\ is added to your environment variables.

What is Git Bash and why do I need it on Windows?

Git Bash is a terminal emulator bundled with Git for Windows. It provides a bash shell environment that emulates Unix-style commands (like ls, mkdir, grep, and cat). Running Git commands in Git Bash ensures cross-platform consistency for workflows and scripts.

Can I install multiple versions of Git on a single computer?

While possible, it is not recommended for general use. The system PATH variable points to a single Git executable at a time. If you require testing across different Git versions, you can compile custom binaries and run them by calling their absolute paths (e.g., /usr/local/bin/git-test).

Does installing Git require a GitHub account?

No. Git is a standalone, local version control program. You do not need a GitHub account, internet connection, or third-party subscription to track files locally. A GitHub account is only required when you want to host your repositories online and share code with collaborators.


Test Your Knowledge

Loading quiz…