Sub topic 6
Checking that Git is installed
This chapter assumes you have finished an install from the previous chapter, can open a terminal (Terminal, PowerShell, Command Prompt, or Git Bash), and know what a shell command and the PATH variable are. You don't need to know any Git commands yet.
Installing a program and being able to run it are different things. A package manager can finish with no errors while your terminal still can't find git. Checking takes a few seconds and tells you three things: whether the shell can find Git, which Git it found, and whether that version is new enough for what you plan to do.
The basic check
Run this in a terminal:
git --versionA working install prints one line. The exact text depends on how Git was packaged:
| Platform and source | Typical output |
|---|---|
| Debian or Ubuntu package | `git version 2.43.0` |
| Homebrew on macOS | `git version 2.47.1` |
| Apple's bundled Git (Xcode Command Line Tools) | `git version 2.39.5 (Apple Git-154)` |
| Git for Windows | `git version 2.47.1.windows.1` |
The first three numbers are the version in major.minor.patch form. Anything after them is a vendor suffix. Apple adds a build tag, and Git for Windows appends .windows.N. Treat the suffix as informational. If you compare versions in a script, strip it first.
The command exits with status 0 when it works. You can read the exit status in your shell to confirm:
git --version
echo $?In PowerShell, use $LASTEXITCODE. In Command Prompt, use echo %ERRORLEVEL%.
git --version
$LASTEXITCODEgit --version and git version do the same thing. Both work, and tutorials use them interchangeably.
What the failure messages mean
If the shell can't find an executable named git, it tells you in its own wording:
| Shell | Message |
|---|---|
| bash | `bash: git: command not found` |
| zsh | `zsh: command not found: git` |
| PowerShell | `git : The term 'git' is not recognized as the name of a cmdlet, function, script file, or operable program.` |
| Command Prompt | `'git' is not recognized as an internal or external command, operable program or batch file.` |
All four mean the same thing: no directory listed in PATH contains a git executable. They do not tell you whether Git is missing from the disk or merely missing from PATH. The next sections separate those two cases.
How the shell finds Git
PATH is an environment variable holding an ordered list of directories. When you type git, the shell looks through those directories from left to right and runs the first match. Two consequences follow:
- If Git is installed but its directory is not in
PATH, the command fails even though the files exist. - If several copies of Git exist, the one in the earliest
PATHdirectory wins, and that may not be the one you just installed.
You can ask the shell which file it would run.
On macOS and Linux:
command -v git
type -a gitcommand -v git prints the path of the first match. type -a git lists every match in PATH order. The Linux utility which -a git does the same on most systems, but command -v and type are built into the shell and behave more consistently.
On Windows PowerShell:
Get-Command git -All | Select-Object Name, Source, Version
where.exe gitWrite where.exe with the extension. In PowerShell, plain where is an alias for Where-Object, so it does not search PATH and appears to do nothing. In Command Prompt you can type where git.
Typical results:
| Platform | Typical path |
|---|---|
| Debian, Ubuntu, Fedora package | `/usr/bin/git` |
| Homebrew, Apple silicon Mac | `/opt/homebrew/bin/git` |
| Homebrew, Intel Mac | `/usr/local/bin/git` |
| Apple's bundled Git | `/usr/bin/git` |
| Git for Windows (default install) | `C:\Program Files\Git\cmd\git.exe` |
Finding out more about the installation
Git can report where it keeps its own files. These commands help when you suspect you are running a different Git than you intended.
git --exec-path
git --html-path
git version --build-optionsgit --exec-pathprints the directory holding Git's helper programs. Git commands likegit commitare dispatched to programs there.git --html-pathprints where the HTML documentation lives, if it was installed.git version --build-optionsprints the version plus details of how this binary was built: the CPU architecture, the commit it was built from, and thesizeof-longandsizeof-size_tvalues. Include this output in a bug report.
The --exec-path output is a quick way to tell installs apart. A Homebrew Git points into the Homebrew prefix. Apple's Git points inside the Xcode or Command Line Tools directory. Git for Windows points under C:\Program Files\Git.
Platform notes
macOS: the `/usr/bin/git` shim
On a Mac that has no developer tools, /usr/bin/git exists but is only a stub. Running git --version there opens a dialog asking you to install the Command Line Tools. If you accept, the install runs and later git calls work. If you cancel, you may see this error:
xcode-select: note: No developer tools were found, requesting install.You can check the active developer directory with:
xcode-select -pIf you installed Git with Homebrew, make sure Homebrew's bin directory comes before /usr/bin in PATH. Otherwise git --version reports the older Apple build. Then type -a git shows both copies, with the Apple one first, and git --version ends in (Apple Git-...). Fix the order in your shell profile, then open a new terminal.
Windows: stale `PATH` in an open terminal
A process reads PATH once, when it starts. If you installed Git while a terminal was open, that terminal still has the old PATH. Close it and open a new one. If you used an editor with a built-in terminal (such as VS Code), restart the editor, not just the terminal tab.
During the Git for Windows installer, one screen asks how Git should be added to PATH. The default, "Git from the command line and also from 3rd-party software", puts C:\Program Files\Git\cmd in PATH, which makes git work in PowerShell and Command Prompt. The most restrictive option makes git available only inside Git Bash. If git works in Git Bash but not in PowerShell, that choice is the likely cause. Re-running the installer and choosing the default option fixes it.
Windows Subsystem for Linux
WSL is a separate Linux environment. A Git installed in Windows does not count as a Git inside your WSL distribution, and the reverse is also true. Check from inside each environment you plan to use. If you work in both, you end up with two separate installs, each with its own configuration.
Linux: older packages
Distribution packages follow the distribution's release schedule, so some are several minor versions behind. This is usually fine. It matters if a tutorial or a setting needs a newer Git. Check the version, and if you need a newer one, use your distribution's backports or a trusted third-party repository. Do not copy a binary into /usr/bin by hand, because the package manager then loses track of it.
Does the version matter?
For this tutorial series, mostly no, with one exception. The init.defaultBranch setting, which a later chapter uses, was added in Git 2.28. On older versions Git ignores that setting, so new repositories still start on master. Other features you will meet later also arrived in specific versions. For example, git switch and git restore were added in 2.23. If something in a tutorial doesn't work, comparing your version with the one the feature requires is a quick diagnostic.
As a rule of thumb, aim for a release from the last couple of years. Security fixes go into current releases, so a very old Git is a risk when you clone repositories from other people.
Checking in a script
Setup scripts, CI jobs, and onboarding tools often need to test for Git. Use command -v to test for existence and compare versions numerically.
#!/usr/bin/env bash
set -euo pipefail
required="2.28.0"
if ! command -v git >/dev/null 2>&1; then
echo "git was not found on PATH" >&2
exit 1
fi
# Keep only major.minor.patch, dropping suffixes such as "(Apple Git-154)" or ".windows.1".
current="$(git --version | sed -E 's/^git version ([0-9]+\.[0-9]+\.[0-9]+).*/\1/')"
lowest="$(printf '%s\n%s\n' "$required" "$current" | sort -V | head -n 1)"
if [ "$lowest" != "$required" ]; then
echo "git $current is older than required $required" >&2
exit 1
fi
echo "git $current is OK (path: $(command -v git))"Notes on this script:
command -v git >/dev/null 2>&1succeeds or fails silently, so it works well inside anif.- Comparing versions as plain strings goes wrong. As text,
2.9.0sorts after2.28.0.sort -Vsorts version numbers numerically, so it gets this right. sort -Vis available in GNUsortand in current macOS. Very old systems may lack it.- The
sedexpression assumes the output starts withgit versionfollowed by three numbers. Some builds show only two numbers for a.0release, such asgit version 2.40. If you target such builds, loosen the pattern or pad the missing part.
The same existence check in PowerShell:
$git = Get-Command git -ErrorAction SilentlyContinue
if (-not $git) {
Write-Error "git was not found on PATH"
exit 1
}
"Found git at $($git.Source)"
git --versionA troubleshooting flow
When the check fails, work through the questions in this order. The first two rule out the most common causes before you reinstall anything.
Some practical points for each step:
- Open a new terminal. This fixes the stale
PATHproblem and costs nothing. - Look for the file. On Linux and macOS, check likely locations such as
/usr/bin/git,/usr/local/bin/git, or/opt/homebrew/bin/git. On Windows, checkC:\Program Files\Git\cmd\git.exe. If the file is there, the install succeeded and onlyPATHis wrong. - Fix
PATHin the right place. On macOS and Linux, edit your shell profile (~/.zshrc,~/.bashrc, or~/.profile, depending on your shell). On Windows, use the "Edit the system environment variables" dialog, then open a new terminal. - Reinstall only after the file check. If the file does not exist, the install failed, and the package manager's output usually says why. Common causes are a missing
sudo, a blocked network, or an unfinished Command Line Tools dialog.
Beyond the version: a first working test
git --version only proves the executable runs. It does not touch your configuration or any repository. A slightly stronger check is to ask Git to read its own settings:
git config --list --show-originOn a fresh install this may print nothing, or only system-level entries. That is normal. Either result confirms that Git can start, read its configuration files, and exit cleanly. Setting your identity and default branch is the subject of the next chapter, so you don't need to change anything yet.
You can also confirm that the help system is installed, since a broken install sometimes lacks it:
git help -a | head -n 5If this lists commands, Git's command dispatch is working. On Windows PowerShell, replace head -n 5 with Select-Object -First 5.
Common mistakes
- Checking in the wrong environment. Git installed in Windows is not visible inside WSL, and a Git in one container or virtual machine is not visible in another.
- Trusting the installer's success message. An installer can succeed while your open terminal still has the old
PATH. - Ignoring duplicate installs. If
type -a gitorwhere.exe gitlists more than one path, the version you see comes from the first. Updating the second changes nothing. - Comparing versions as text. Use a numeric comparison such as
sort -V. - Using
wherein PowerShell. It is an alias for a different command. Usewhere.exeorGet-Command.
Summary
git --versionprints the installed version and exits with status 0 when Git works.- A "command not found" or "not recognized" error means no directory in
PATHholds agitexecutable. It does not prove Git is absent from the disk. command -v git,type -a git,Get-Command git -All, andwhere.exe gitshow which copy you are running and whether there are duplicates.- Open a new terminal after installing, especially on Windows, because
PATHis read when a process starts. - Version suffixes such as
(Apple Git-154)and.windows.1are vendor details. Strip them before comparing versions in scripts. - Git 2.28 or newer is needed for the
init.defaultBranchsetting used in later chapters.
Exercise
You installed Git for Windows with winget while a PowerShell window was open. In that same window, git --version prints "'git' is not recognized". In a freshly opened PowerShell window, it works and prints git version 2.47.1.windows.1. What explains the difference, and what command would you use in PowerShell to list every Git executable on the PATH?
Show answer
The first PowerShell process read PATH when it started, before the installer added C:\Program Files\Git\cmd, so it never saw the new entry. A new window reads the updated PATH. To list every match, run Get-Command git -All (or where.exe git, with the .exe, since plain where is an alias for Where-Object in PowerShell).
Exercise
On a Mac you installed Git with Homebrew, but git --version prints git version 2.39.5 (Apple Git-154). What does that tell you, and how do you confirm and fix it?
Show answer
The shell is running Apple's bundled Git from /usr/bin/git, not the Homebrew copy, so /usr/bin comes before Homebrew's bin directory in PATH. Confirm with type -a git, which lists both paths in PATH order. Fix it by putting Homebrew's directory (/opt/homebrew/bin on Apple silicon, /usr/local/bin on Intel) ahead of /usr/bin in your shell profile, then open a new terminal and run git --version again.
Exercise
Why does the version-check script use sort -V instead of comparing the version strings directly, and what would go wrong with a plain string comparison of 2.9.0 and 2.28.0?
Show answer
A plain string comparison goes character by character, so "2.9.0" sorts after "2.28.0" because "9" is greater than "2". That wrongly treats 2.9.0 as newer. sort -V compares each dot-separated part as a number, so 2.28.0 is correctly seen as newer than 2.9.0.