Installation & Setup
Git runs on every major operating system. Installation takes under 2 minutes, and first-time setup requires just two commands (user.name and user.email). After that, you're ready to initialize repositories and start tracking changes.
Install Git
- Ubuntu / Debian
- CentOS / RHEL / Fedora
- macOS
- Windows
sudo apt update
sudo apt install -y git
# CentOS / RHEL
sudo yum install -y git
# Fedora
sudo dnf install -y git
# Option 1: Xcode Command Line Tools (usually pre-installed)
xcode-select --install
# Option 2: Homebrew
brew install git
Download from git-scm.com/downloads and run the installer.
Or use winget:
winget install --id Git.Git -e --source winget
Verify Installation
git --version
Expected output (example):
git version 2.43.0
For full feature support (including partial clone, sparse checkout, and improved performance), use Git 2.30+. Most modern Linux distributions ship with 2.40+.
First-Time Configuration
Git needs your identity for commit authorship. This is required before you can make any commits:
# Set your name (shown in every commit)
git config --global user.name "Your Name"
# Set your email (must match your GitHub account for linking)
git config --global user.email "your.email@example.com"
Recommended Global Settings
# Set default branch name to 'main' (instead of 'master')
git config --global init.defaultBranch main
# Set default editor (for commit messages, rebase, etc.)
git config --global core.editor "nano" # or: vim, code --wait
# Enable colored output
git config --global color.ui auto
# Set pull strategy to rebase (cleaner history)
git config --global pull.rebase true
# Set push default to current branch
git config --global push.default current
# Handle line endings (Linux/macOS)
git config --global core.autocrlf input
# Handle line endings (Windows)
# git config --global core.autocrlf true
Verify Your Configuration
git config --global --list
Expected output (example):
user.name=donnyaw
user.email=donnyaw@gmail.com
init.defaultbranch=main
core.editor=nano
color.ui=auto
pull.rebase=true
push.default=current
core.autocrlf=input
Configuration Levels
Git has three configuration levels, each overriding the previous:
| Level | Flag | File Location | Scope |
|---|---|---|---|
| System | --system | /etc/gitconfig | All users on the machine |
| Global | --global | ~/.gitconfig | Your user account |
| Local | --local | .git/config (inside repo) | This repository only |
# View all configs with their origin
git config --list --show-origin
If you use different emails for work vs personal projects, set the email at the local level inside each repository:
cd ~/work/project
git config user.email "your.name@company.com"
Create Your First Repository
# Create a project directory
mkdir my-first-repo
cd my-first-repo
# Initialize Git
git init
Expected output:
Initialized empty Git repository in /home/user/my-first-repo/.git/
Make Your First Commit
# Create a file
echo "# My First Project" > README.md
# Stage the file
git add README.md
# Commit
git commit -m "Initial commit"
Expected output:
[main (root-commit) a1b2c3d] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
Essential .gitignore
Create a .gitignore file to exclude files that shouldn't be tracked:
# .gitignore
# OS files
.DS_Store
Thumbs.db
# IDE / editor files
.vscode/
.idea/
*.swp
*.swo
# Dependencies
node_modules/
vendor/
__pycache__/
*.pyc
# Environment files
.env
.env.local
.env.*.local
# Build output
dist/
build/
*.o
*.exe
# Logs
*.log
npm-debug.log*
# Secrets
*.pem
*.key
id_rsa
For OS-specific files you always want ignored:
git config --global core.excludesFile ~/.gitignore_global
echo ".DS_Store" >> ~/.gitignore_global
echo "Thumbs.db" >> ~/.gitignore_global
echo "*.swp" >> ~/.gitignore_global
Troubleshooting
| Problem | Likely Cause | Fix |
|---|---|---|
git: command not found | Git not installed or not in PATH | Install Git, restart terminal |
Author identity unknown | user.name / user.email not set | Run git config --global user.name "..." |
fatal: not a git repository | Not inside a Git repo | Run git init or cd to a repo |
| Wrong default branch name | Old Git version uses master | Set git config --global init.defaultBranch main |
Best Practices
- Set your identity immediately after installing Git
- Use global config for defaults, local config for per-repo overrides
- Always create a
.gitignorebefore your first commit - Use
mainas your default branch name (modern convention) - Configure your preferred editor for commit messages
What's Next
- SSH Setup — Set up SSH keys for secure GitHub authentication
- Git Configuration (Deep Dive) — Advanced configuration, aliases, and optimization