Sub topic 5
Installing Git on your operating system
Git is distributed as a small set of command-line programs. You can install it with a package manager, an installer, or a source build. This chapter covers the standard route on Windows, macOS, and Linux. It also covers what each route puts on your machine and how to resolve conflicts when several copies of Git exist.
Prerequisites and scope
This chapter assumes you can:
- Open a terminal or command prompt and run simple commands.
- Use
sudoon Linux or macOS, or run an installer with administrator rights on Windows. - Read a
PATHsetting at a basic level.PATHis the list of directories your shell searches when you type a command name.
It covers installing and upgrading Git. Confirming the install in detail, setting your identity, and creating a repository are covered in later chapters. The commands here end with a quick version check, so you can tell whether the install worked.
What you are installing
Git is a command-line program named git. Many of its subcommands (git add, git commit, and so on) are separate helper programs that git finds in its own installation directory. Installing Git puts several things on your machine:
- The
gitexecutable and its helper programs. - Manual pages and built-in help files.
- Default templates used when you create a repository.
- On some platforms, extras such as a graphical history viewer (
gitk), a commit tool (git gui), or a bundled shell.
A package manager adds one more benefit. It records what it installed, so you can upgrade or remove Git with one command. That is the main reason to prefer a package manager over downloading files by hand.
Choosing a method
Most people should use the package manager that already manages other software on their system. The diagram shows the usual decision.
| Platform | Typical method | Command | Version you get |
|---|---|---|---|
| Windows | winget | `winget install --id Git.Git -e --source winget` | Current Git for Windows release |
| Windows | Installer from git-scm.com | Run the downloaded `.exe` | Current Git for Windows release |
| macOS | Homebrew | `brew install git` | Recent upstream release |
| macOS | Xcode Command Line Tools | `xcode-select --install` | Apple's build, often older |
| Debian, Ubuntu | apt | `sudo apt install git` | Whatever your release ships |
| Fedora | dnf | `sudo dnf install git` | Usually recent |
| Arch | pacman | `sudo pacman -S git` | Very recent |
| openSUSE | zypper | `sudo zypper install git` | Distribution version |
Installing on Windows
Windows does not ship with Git. The project that provides it is called Git for Windows. It bundles Git, a Bash shell (Git Bash), and a set of Unix tools that Git relies on internally.
With winget
winget is the Windows Package Manager. It is included in current versions of Windows 10 and 11. Run this in PowerShell or Command Prompt:
winget install --id Git.Git -e --source wingetThe flags matter:
--id Git.Gitselects the package by its exact identifier instead of searching by name.-e(exact) makes winget match that identifier exactly, so a similarly named package is not chosen by mistake.--source wingetlimits the search to the main winget catalog.
When it finishes, close the terminal and open a new one. A terminal reads PATH only when it starts, so a window opened before the install will not find git.
With the graphical installer
You can also download the installer from git-scm.com and run it. It asks a series of questions. For most people the defaults are fine, but a few choices have lasting effects:
- Default editor. Git opens an editor when you write a commit message. The installer's default is Vim, which surprises many people. If you do not know Vim, pick another editor such as Notepad++ or Visual Studio Code. You can change it later with
git config. - Initial branch name. You can leave this as "Let Git decide" or choose to override it with
main. A later chapter in this tutorial sets this withgit config, so either choice is easy to change. - PATH environment. The recommended option, "Git from the command line and also from 3rd-party software", adds Git to
PATHso it works in PowerShell, Command Prompt, and editors. The most restrictive option makesgitavailable only inside Git Bash, which causes "command not found" errors elsewhere. - Line endings. Windows ends text lines with two characters (CRLF). Linux and macOS use one (LF). The default, "Checkout Windows-style, commit Unix-style line endings", converts them for you. It works well for mixed teams. It is also a common source of confusing diffs if your team has its own rules.
- Credential helper. Leave Git Credential Manager enabled. It stores your login for hosting services securely, so you do not retype passwords.
The installer also lets you choose a terminal for Git Bash. The default, MinTTY, is fine.
Updating on Windows
Run winget upgrade --id Git.Git -e to update a winget install. Git for Windows also includes the command git update-git-for-windows, which checks for a newer release and starts the installer.
Installing on macOS
macOS offers two common paths, and they can coexist. That coexistence is the main source of confusion.
Apple's copy through the Command Line Tools
If you type git in Terminal on a Mac that has no developer tools, macOS offers to install the Xcode Command Line Tools. You can start that install directly:
xcode-select --installThis installs a build of Git that Apple compiled and maintains. It is convenient because it needs no extra software. The drawback is that Apple's version usually trails the upstream release by some months.
Homebrew
Homebrew is a third-party package manager for macOS. If you have it, installing Git takes one command:
brew install gitHomebrew installs the upstream release and upgrades it with brew upgrade git. The install location depends on your Mac's processor:
- Apple silicon:
/opt/homebrew/bin/git - Intel:
/usr/local/bin/git
Which copy runs?
After installing with Homebrew, you may have two copies: Apple's at /usr/bin/git and Homebrew's. Your shell runs the first one it finds on PATH. Homebrew's setup instructions put its directory ahead of /usr/bin, so Homebrew's Git normally wins. You can check with:
which -a git
git --versionwhich -a lists every git the shell can find, in search order. The first line is the one that runs. If it shows /usr/bin/git and you wanted Homebrew's version, your PATH is missing the Homebrew directory. Add the line that brew printed during its own setup to your shell profile (~/.zprofile for zsh), then open a new terminal.
Installing on Linux
Linux distributions package Git in their own repositories, so the install is one command. Refresh the package index first on Debian-based systems, because a stale index can point to a package version that no longer exists on the mirror.
# Debian or Ubuntu
sudo apt update
sudo apt install git
# Fedora
sudo dnf install gitOther distributions follow the same pattern:
# Arch Linux
sudo pacman -S git
# openSUSE
sudo zypper install git
# Alpine Linux
sudo apk add gitOn Debian and Ubuntu, a package named git-all also exists. It adds optional tools such as email integration and SVN bridges. The plain git package is what you want.
Old versions on stable distributions
Long-term-support distributions freeze package versions at release time. An Ubuntu LTS install can be a few years behind upstream, and Debian stable can be further behind. For everyday work an older Git is usually fine, since the core commands change slowly. It becomes a problem when a tutorial or tool needs a newer feature.
If you need a newer build on Ubuntu, the Git maintainers publish a package archive:
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install gitThis replaces the distribution's version with the newer one and keeps it updated through apt. Adding a third-party archive means trusting its publisher, so do this only when you actually need the newer version.
Building from source
You can also compile Git from the tarballs published at kernel.org. This gives you the newest release and a custom install location. It also means you must install compiler tools and library headers yourself, and you must handle upgrades manually, because no package manager tracks the install. Treat it as a last resort. The usual reasons are a locked-down server with no repository access or a need for a specific patched version.
Installing without administrator rights
Sometimes you cannot use sudo or run an installer as administrator, for example on a shared server or a managed work laptop. You have a few options:
- On Windows, Git for Windows offers a per-user install. The installer asks for elevation by default, but a user-level install is also possible, and it installs into your profile folder.
- On Linux or macOS, a source build with
prefix=$HOME/.localinstalls into your home directory. You then add$HOME/.local/bintoPATH. - Conda and similar environment managers can supply Git in a user-owned environment.
Each of these leaves you responsible for updates, so note where you put the install.
Common problems
Most installation problems come down to PATH, permissions, or a stale package index.
| Symptom | Likely cause | Fix |
|---|---|---|
| `git: command not found` right after install | The terminal was open before the install | Close it and open a new one |
| `git` works in Git Bash but not in PowerShell | `PATH` option was set to Git Bash only | Rerun the installer and choose the recommended `PATH` option |
| `Unable to locate package git` on Ubuntu | Package index is out of date | Run `sudo apt update`, then install again |
| Mac shows an old version after `brew install git` | `/usr/bin/git` is ahead of Homebrew on `PATH` | Check with `which -a git` and fix the `PATH` order |
| `winget` is not recognized | Older Windows build without App Installer | Use the graphical installer instead |
Checking the result
Whichever route you chose, confirm the install in a new terminal window. These two commands show which executable runs and which version it is:
git --versionOn Windows PowerShell, Get-Command git shows the path, and in Command Prompt where git does the same. On macOS and Linux, use command -v git or which -a git. A version string such as git version 2.47.0 means the install worked. The next chapter looks at this check in more detail.
Summary of decisions
- Use the package manager your system already uses: winget, Homebrew, apt, dnf, or another.
- On Windows, accept the recommended
PATHoption, and change the default editor if you do not know Vim. - On macOS, prefer Homebrew if you want a current version, and use
which -a gitto see which copy runs. - On Linux, accept the distribution's version unless you need a newer feature. In that case, use a maintained archive before you consider a source build.
- Always open a new terminal before testing.
Exercise
You installed Git on a Mac with brew install git, but git --version still prints an old Apple-built version. Which command shows why, and what is the likely cause?
Show answer
Run which -a git. It lists every git on PATH in search order. The likely cause is that /usr/bin/git (Apple's copy) comes before Homebrew's directory (/opt/homebrew/bin on Apple silicon, /usr/local/bin on Intel) in PATH. Fix it by adding Homebrew's directory to the front of PATH in your shell profile, then open a new terminal.
Exercise
On Windows you ran the winget command, and the install succeeded, but git --version in the same PowerShell window says the command is not recognized. Name the cause and the fix.
Show answer
The terminal was started before the install, so it still holds the old PATH, which does not include Git. Close the window and open a new one. If git is still missing there, rerun the installer and select the "Git from the command line and also from 3rd-party software" PATH option.
Exercise
A teammate on Ubuntu LTS needs a newer Git than the one apt install git provides. What is the safer first option, and what is the trade-off?
Show answer
Add the Git maintainers' PPA with sudo add-apt-repository ppa:git-core/ppa, run sudo apt update, and then sudo apt install git. The newer version then updates through apt like any other package. The trade-off is that you now trust a third-party archive. A source build gives the same version but has no automatic updates.