How to Install Git on macOS: Xcode Tools, Homebrew and Setup
macOS makes Git available through Apple’s Xcode Command Line Tools, and many Macs already have it. The main decision is whether that version is enough, or whether you want a current upstream Git from Homebrew — and, if you install both, knowing which one you are actually running.
Step 1 — Check what you already have
Section titled “Step 1 — Check what you already have”Open Terminal and run:
git --versionWhat it doesPrints the version of Git currently on your PATH.
Why we run itMany Macs already have Git via the Xcode Command Line Tools. Checking first tells you whether you need to install anything at all.
Expected resultEither a version string, or a dialog offering to install the command line developer tools — see below.
Three outcomes are possible.
A version prints. Git is installed. Note the number; Apple’s build typically shows an Apple-specific
suffix such as git version 2.39.5 (Apple Git-154).
A dialog appears saying the git command requires the command line developer tools. This is macOS
offering to install them. Click Install, or cancel and use one of the methods below.
command not found. No Git and no developer tools. Continue to the next step.
Step 2 — Install Git
Section titled “Step 2 — Install Git”Two supported routes. Read both before choosing.
Apple’s developer tools package includes Git along with compilers and other build tooling.
xcode-select --installA dialog appears; confirm it and wait for the download to finish. Then verify:
git --versionWhen to choose this: you want the simplest, Apple-supported route, or you need the other developer tools anyway. Many other tools — including Homebrew — require these tools, so installing them is rarely wasted.
The trade-off: Apple’s bundled Git lags upstream, sometimes by a year or more. It is a complete, working Git and everything in this curriculum works with it. Newer Git features simply will not be available.
Homebrew is the community package manager for macOS and tracks upstream Git closely.
If you do not already have Homebrew, install it using the command published on brew.sh — always take it from the official site rather than copying it from a tutorial, since it is a script that runs with your privileges.
Then:
brew install gitVerify:
git --versionTo upgrade later:
brew updatebrew upgrade gitWhen to choose this: you want current Git, or you already manage other developer tooling with Homebrew.
The trade-off: an extra tool to maintain, and the PATH question covered in the next section.
If you already use MacPorts rather than Homebrew:
sudo port install gitThe same PATH considerations apply.
Step 3 — Know which Git you are running
Section titled “Step 3 — Know which Git you are running”If you install Homebrew’s Git alongside Apple’s, both exist on disk and PATH order decides which one
runs. This is the most common source of “I upgraded Git but the version did not change.”
which gitWhat it doesPrints the full path of the git executable your shell will actually run.
Why we run itIt resolves the ambiguity directly. git --version tells you what is running; which git tells you where it came from.
Expected resultA path. Apple's is /usr/bin/git. Homebrew's is /opt/homebrew/bin/git on Apple silicon, or /usr/local/bin/git on Intel Macs.
To see every git on your PATH, in the order the shell would try them:
which -a git/opt/homebrew/bin/git/usr/bin/gitThe first line wins. If Homebrew’s Git is installed but /usr/bin/git appears first, Homebrew’s
directory is not early enough on your PATH. Homebrew’s installer normally configures this; if it did
not, add it to your shell profile:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofileeval "$(/opt/homebrew/bin/brew shellenv)"echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profileeval "$(/opt/homebrew/bin/brew shellenv)"On Intel Macs substitute /usr/local/bin/brew. brew shellenv prints the correct settings for your
installation, so it adapts automatically rather than hardcoding a path.
Step 4 — Configure your identity
Section titled “Step 4 — Configure your identity”-
Set your name.
Terminal window git config --global user.name "Your Name" -
Set your email.
Terminal window git config --global user.email "you@example.com" -
Confirm both.
Terminal window git config --global --list
Step 5 — Set the default branch name
Section titled “Step 5 — Set the default branch name”git config --global init.defaultBranch mainOlder Git versions — including some Apple builds — create master by default and print a hint about
it. This setting makes new repositories use main, matching current convention everywhere.
Step 6 — Choose an editor
Section titled “Step 6 — Choose an editor”git config --global core.editor "nano"Included with macOS and easy to leave: Ctrl+O to save, Ctrl+X to exit.
git config --global core.editor "code --wait"Requires the code command, installed from VS Code via Command Palette → Shell Command: Install ‘code’ command in PATH. The --wait flag is required.
git config --global core.editor "vim"Write and quit with :wq. Abort with :q!, which cancels the commit.
Step 7 — Credential handling
Section titled “Step 7 — Credential handling”macOS provides an encrypted credential store — the Keychain — and Git integrates with it through the
osxkeychain credential helper. Both Apple’s Git and Homebrew’s Git include it.
git config --global credential.helper osxkeychainAfter this, the first HTTPS push prompts for credentials once and stores them in the Keychain. Subsequent operations retrieve them automatically.
Check what is configured:
git config --get credential.helperTo remove a stored credential after rotating a token, open Keychain Access, search for the host name, and delete the entry. Or from the terminal:
git credential-osxkeychain eraseThen type host=github.com, press Enter twice.
SSH as an alternative
Section titled “SSH as an alternative”Rather than HTTPS and tokens, you can authenticate with an SSH key pair:
ssh-keygen -t ed25519 -C "you@example.com"This writes ~/.ssh/id_ed25519 (private — never share it) and ~/.ssh/id_ed25519.pub (public — safe
to upload to a hosting provider). macOS can store the key’s passphrase in the Keychain so you are not
prompted repeatedly:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519SSH key management is a topic in its own right and will be covered properly in a future GitHub Engineering pillar. HTTPS with the Keychain helper is a perfectly good starting point.
Inspecting your configuration
Section titled “Inspecting your configuration”git config --list --show-scope --show-originOn macOS the scopes typically resolve to:
| Scope | Typical location |
|---|---|
| System | /etc/gitconfig, or /opt/homebrew/etc/gitconfig for Homebrew’s Git |
| Global | ~/.gitconfig |
| Local | .git/config inside the repository |
Local overrides global, and global overrides system.
Updating Git
Section titled “Updating Git”Apple’s Git updates with macOS system updates. There is no separate command. To force a reinstall of the tools:
sudo rm -rf /Library/Developer/CommandLineToolsxcode-select --installbrew updatebrew upgrade gitbrew update refreshes Homebrew’s package definitions; brew upgrade git installs the newer version.
sudo port selfupdatesudo port upgrade gitTroubleshooting
Section titled “Troubleshooting”A dialog says the git command requires developer tools. Expected on a Mac without them. Click
Install, or run xcode-select --install.
git --version shows an old version after brew install git. A PATH ordering problem. Run
which -a git and confirm Homebrew’s directory comes first. See
Step 3.
xcode-select: error: command line tools are already installed. They are present. Run
git --version to confirm Git works. To reinstall, remove
/Library/Developer/CommandLineTools first.
Author identity unknown when committing.
*** Please tell me who you are.fatal: unable to auto-detect email addressuser.name and user.email are unset in an applicable scope. Return to
Step 4 and use --global.
brew: command not found. Homebrew is installed but not on your PATH. Run the brew shellenv
commands from Step 3.
Credentials prompted on every push. No credential helper is configured. Run
git config --global credential.helper osxkeychain.
fatal: detected dubious ownership in repository. Git refuses to operate on a repository owned by
a different user — a safety check. If the ownership is legitimate:
git config --global --add safe.directory /path/to/repoAdd specific paths rather than disabling the check.
Case-sensitivity surprises. The default macOS filesystem is case-insensitive but
case-preserving. Renaming Readme.md to README.md may not register as a change. Force it
explicitly:
git mv --force Readme.md README.mdThis matters when collaborating with Linux users, whose filesystems treat those as two distinct files.
Uninstalling Git
Section titled “Uninstalling Git”Apple’s Git is part of the developer tools and is not separately removable. To remove the whole package:
sudo rm -rf /Library/Developer/CommandLineToolsNote that this removes compilers and other tooling that other software may depend on.
brew uninstall gitIf Apple’s Git is also installed, git continues to work afterwards — it falls back to /usr/bin/git.
sudo port uninstall gitWhat You Learned
Section titled “What You Learned”- macOS offers Git through Xcode Command Line Tools (
xcode-select --install) or Homebrew (brew install git). - Apple’s Git lags upstream but is complete and sufficient for this curriculum.
- With both installed,
PATHorder decides which runs —which -a gitshows the resolution. - Homebrew lives at
/opt/homebrewon Apple silicon and/usr/localon Intel;brew shellenvconfigures the right one. credential.helper osxkeychainstores credentials in the encrypted macOS Keychain.- The default macOS filesystem is case-insensitive, which affects case-only renames.
Try It Yourself
Section titled “Try It Yourself”- Run
which -a git. If more than one path appears, you have multiple installations — identify which one wins. - Run
git --versionand note whether the output includes an(Apple Git-…)suffix. - Set your identity with
--global, then confirm withgit config --global --list. - Create a scratch directory, run
git init, and confirm the branch is namedmain.
Next Lesson
Section titled “Next Lesson”Your environment is ready. The next lesson is a full hands-on tutorial: you will build a repository from nothing, make two commits, and understand exactly what each command did.