Git Notes
Learn how to install Git on macOS using Homebrew, Xcode Command Line Tools, MacPorts, or from source with SSH key configuration, terminal integration, and Mac-specific troubleshooting.
macOS provides multiple ways to install Git. This guide covers Homebrew (recommended), Xcode tools, MacPorts, and building from source.
Pre-Check
git --version
# If command not found, proceed with installationMethod 1: Homebrew (RECOMMENDED)
Homebrew is the macOS package manager with latest Git versions and easy updates.
Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Install Git
brew install git
git --version
# Output: git version 2.44.0
# Update later
brew upgrade gitAdvantages:
- Latest versions available
- Simple updates with
brew upgrade - Works on Intel and Apple Silicon Macs
- Active community support
Method 2: Xcode Command Line Tools (Simplest)
Fastest method, though versions lag behind releases.
xcode-select --install
git --version
# Check location
xcode-select -pWhen to use:
- Quick setup needed
- Only basic Git functionality
- Already have Xcode installed
- Limited disk space
Limitations:
- Updates lag behind official releases
- Updates tied to macOS OS updates
- May be outdated (e.g., 2.37 when latest is 2.44)
Method 3: MacPorts
sudo port selfupdate
sudo port install git
git --versionMethod 4: Build from Source
# Install dependencies
brew install curl-openssl expat gettext perl autoconf
# Download
cd /tmp
curl -L https://kernel.org/pub/software/scm/git/git-2.44.0.tar.gz -o git.tar.gz
tar -xzf git-2.44.0.tar.gz
cd git-2.44.0
# Configure & compile
./configure --prefix=/usr/local \
--with-openssl=/usr/local/opt/openssl \
--with-curl=/usr/local/opt/curl-openssl \
--with-expat=/usr/local/opt/expat
make
sudo make install
git --versionPost-Installation Configuration
Basic Setup
git config --global user.name "Your Full Name"
git config --global user.email "your@email.com"
git config --global init.defaultBranch main
git config --global core.editor "nano"macOS-Specific
# Use macOS Keychain (RECOMMENDED)
git config --global credential.helper osxkeychain
# Line endings
git config --global core.autocrlf input
# Global gitignore
git config --global core.excludesfile ~/.gitignore_global
# Create gitignore
cat > ~/.gitignore_global << 'EOF'
.DS_Store
.AppleDouble
.LSOverride
*.swp
*~
.env
.vscode/
.idea/
node_modules/
venv/
EOFSSH Key Setup
# Generate key
ssh-keygen -t ed25519 -C "your@email.com"
# Start agent
eval "$(ssh-agent -s)"
# Create SSH config
cat > ~/.ssh/config << 'EOF'
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
EOF
chmod 600 ~/.ssh/config
# Add to Keychain
ssh-add -K ~/.ssh/id_ed25519
# Display public key
cat ~/.ssh/id_ed25519.pubVerification
git --version
which git
ls -la $(which git)
git config --listOutput:
Test Repository
mkdir ~/test-git && cd ~/test-git
git init
git config user.name "Test User"
git config user.email "test@example.com"
echo "Hello Git" > README.md
git add README.md
git commit -m "Initial commit"
git log --onelineTerminal Aliases
Add to ~/.zshrc:
alias g="git"
alias gs="git status"
alias ga="git add"
alias gc="git commit -m"
alias gp="git push"
alias gb="git branch"
source ~/.zshrcTerminal Prompt with Branch
Add to ~/.zshrc:
Troubleshooting
"xcrun: error"
xcode-select --reset
xcode-select --install"git: command not found"
xcode-select --install
# or
brew install git"Could not read Username for GitHub"
git config --global credential.helper osxkeychain
git credential-osxkeychain erase host=github.comSSH Permission Denied
ssh-keygen -t ed25519 -C "your@email.com"
ssh-add -K ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub # Add to GitHub
ssh -T git@github.com # TestInstallation Comparison
| Method | Ease | Latest | Updates | Recommended |
|---|---|---|---|---|
| Homebrew | Easy | Yes | Auto | ✓ BEST |
| Xcode | Easiest | Lag | With OS | Good |
| MacPorts | Moderate | Yes | Manual | Alt |
| Source | Hard | Yes | Manual | Advanced |
Upgrading
# Homebrew
brew upgrade git
# Xcode tools
softwareupdate -l
softwareupdate -i -aInterview Q&A
Q1: Best way to install Git on macOS?
A: Homebrew is recommended. First install Homebrew from https://brew.sh, then brew install git. Homebrew provides latest versions with automatic updates. Xcode Command Line Tools (xcode-select --install) simpler but provides older versions.
Q2: How does osxkeychain improve workflow?
A: osxkeychain securely stores GitHub credentials in macOS Keychain. After authenticating once, Git automatically provides stored credentials, eliminating password prompts. More secure than plaintext storage.
Q3: Configuration immediately after installation?
A: Configure identity with git config --global user.name and user.email. Set credential helper to osxkeychain. Configure line endings with core.autocrlf input. Generate SSH keys for GitHub. Create global gitignore for .DS_Store.
Q4: When build from source instead of Homebrew?
A: Build from source for absolute latest features not in Homebrew, specific compilation flags, or Git development. For most developers, Homebrew provides sufficient currency with much less effort.
Q5: How to set up SSH keys on macOS for GitHub?
A: Generate key with ssh-keygen -t ed25519. Create ~/.ssh/config with GitHub settings and keychain integration. Add to Keychain with ssh-add -K. Display with cat ~/.ssh/id_ed25519.pub and add to GitHub Settings. Test with ssh -T git@github.com.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Install Git on macOS - Installation Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, setup, install, macos, install git on macos - installation guide
Related Git & GitHub Topics