Git - Installation & Setup

Beginner

Before you can use Git, you need to install it and configure it with your identity. This guide covers installation on all major operating systems and essential configuration.

Installing Git

Windows Installation

Method 1: Git for Windows (Recommended)

  1. Download from https://git-scm.com/download/win
  2. Run the installer with these recommended settings:
    • ✅ Use Git from the command line and also from 3rd-party software
    • ✅ Use the OpenSSL library
    • ✅ Checkout Windows-style, commit Unix-style line endings
    • ✅ Use MinTTY (the default terminal of MSYS2)
  3. This installs Git Bash, Git GUI, and command line tools

Method 2: Chocolatey Package Manager

# In PowerShell (as Administrator)
choco install git

Method 3: Winget (Windows 10+)

# In PowerShell
winget install Git.Git

macOS Installation

Method 1: Homebrew (Recommended)

# Install Homebrew first: https://brew.sh
brew install git

Method 2: Xcode Command Line Tools

# This installs an older version but works
xcode-select --install

Method 3: Official Installer

Linux Installation

Ubuntu/Debian:

sudo apt update
sudo apt install git

CentOS/RHEL/Fedora:

# CentOS/RHEL
sudo yum install git

# Fedora
sudo dnf install git

Arch Linux:

sudo pacman -S git

Verifying Installation

After installation, verify Git is working:

git --version
Expected Output:
git version 2.42.0
If you see "command not found", restart your terminal or check your PATH environment variable.

Essential Configuration

Git needs to know who you are for commit attribution. This information goes into every commit you make:

Setting Your Identity

Required: Set your name and email
# Replace with your actual name and email
git config --global user.name "John Doe"
git config --global user.email "[email protected]"
Important: Use the same email address you'll use with GitHub, GitLab, or other Git hosting services for proper attribution.

Choosing Your Default Editor

Git will open an editor for commit messages. Choose one you're comfortable with:

Visual Studio Code (Recommended for beginners):
git config --global core.editor "code --wait"
Other popular editors:
# Vim (advanced users)
git config --global core.editor "vim"

# Nano (simple terminal editor)
git config --global core.editor "nano"

# Notepad++ (Windows)
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

# Sublime Text
git config --global core.editor "subl --wait"

Setting Default Branch Name

Use 'main' as default branch (modern standard):
git config --global init.defaultBranch main

Line Ending Configuration

Windows:
git config --global core.autocrlf true
macOS/Linux:
git config --global core.autocrlf input

Viewing Your Configuration

See all configurations:
git config --list
See specific configuration:
git config user.name
git config user.email
Expected Output:
John Doe
[email protected]

Configuration Levels

Git has three configuration levels:

  • --system: Applies to all users on the system
  • --global: Applies to all repositories for your user
  • --local: Applies only to the current repository
Examples:
# Global (most common)
git config --global user.name "John Doe"

# Local (repository-specific)
git config --local user.email "[email protected]"

# View configuration locations
git config --list --show-origin

Setting Up SSH Keys (Recommended)

SSH keys provide secure, passwordless authentication with Git hosting services:

Generate SSH Key

# Generate new SSH key (replace email with yours)
ssh-keygen -t ed25519 -C "[email protected]"
Interactive prompts:
Enter file in which to save the key (/Users/john/.ssh/id_ed25519): [Press Enter]
Enter passphrase (empty for no passphrase): [Type passphrase or Press Enter]
Enter same passphrase again: [Repeat passphrase or Press Enter]

Add SSH Key to SSH Agent

macOS/Linux:
# Start ssh-agent
eval "$(ssh-agent -s)"

# Add key to agent
ssh-add ~/.ssh/id_ed25519
Windows (Git Bash):
# Start ssh-agent
eval $(ssh-agent -s)

# Add key to agent
ssh-add ~/.ssh/id_ed25519

Copy Public Key

Display public key:
# macOS
cat ~/.ssh/id_ed25519.pub | pbcopy

# Linux
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard

# Windows Git Bash
cat ~/.ssh/id_ed25519.pub | clip

# Or just display it
cat ~/.ssh/id_ed25519.pub
Example output to copy:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILmBzgmvDdOyWvN4/1Y6Tr4UbCzSIk9GlXeE2U3+a1b2 [email protected]

Add to GitHub/GitLab

GitHub:

  1. Go to Settings → SSH and GPG keys
  2. Click "New SSH key"
  3. Paste your public key
  4. Give it a descriptive title

Test SSH connection:

Successful connection:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Useful Aliases

Set up shortcuts for common commands:

# Essential aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.lg "log --oneline --graph --decorate --all"

Now you can use short commands:

git st      # instead of git status
git co main # instead of git checkout main
git lg      # beautiful log graph

Additional Helpful Settings

# Show more context in diffs
git config --global diff.context 5

# Use better diff algorithm
git config --global diff.algorithm histogram

# Automatically rebase when pulling
git config --global pull.rebase true

# Push only current branch by default
git config --global push.default simple

# Colorful output
git config --global color.ui auto

Troubleshooting Installation

Command Not Found

  • Windows: Restart terminal, check PATH in Environment Variables
  • macOS: May need to run xcode-select --install
  • Linux: Ensure package installation completed successfully

Permission Issues

# Fix SSH key permissions (Unix-like systems)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

Verification Checklist

Confirm your setup is complete:

  • git --version shows version number
  • git config user.name shows your name
  • git config user.email shows your email
  • git config core.editor shows your preferred editor
  • ✅ SSH key is generated and added to hosting service
  • ssh -T [email protected] authenticates successfully

Next Steps

Great! Git is now installed and configured. Let's create your first repository and learn the basic workflow:

Git - Basic Workflow

For Teachers: Have students verify each step works before moving on. SSH setup often causes issues, so budget extra time for troubleshooting.