A practical, visual guide to mastering Git in your terminal on both Windows and macOS. Learn platform installation, terminal setups, line-ending configurations, the core mental model, everyday commands, branching strategies, stashing, and conflict resolution.
- Platform Installation (Windows & macOS)
- Terminal Selection & Cross-Platform Configuration
- Git Architecture: The 4 Fundamental Areas
- Creating & Cloning Repositories
- The Daily Developer Workflow
- Branching & Merging
- Stashing Changes (Temporary Shelving)
- Undoing Mistakes & Fixing Errors
- Resolving Merge Conflicts
- Cross-Platform .gitignore & Best Practices
You have two easy ways to install Git on Windows:
- Visit git-scm.com/download/win and download the 64-bit installer for Windows.
- Run the
.exeinstaller. - During setup, keep the recommended defaults:
- Text Editor: Choose your preferred editor (VS Code, Notepad++, etc.).
- Default branch name: Choose
main. - Adjusting your PATH: Select "Git from the command line and also from 3rd-party software".
- Line ending conversion: Choose "Checkout Windows-style, commit Unix-style line endings".
- Credential Helper: Choose "Git Credential Manager".
- Finish installation. You now have Git Bash, Git GUI, and command-line Git available.
Open PowerShell or Windows Terminal as Administrator and run:
winget install --id Git.Git -e --source wingetmacOS comes with a stub that can install Git automatically. Open Terminal.app (Press Cmd + Space, type Terminal, and hit Enter), then run:
xcode-select --installClick Install in the software update popup and accept the license.
If you use Homebrew, install the most up-to-date Git package:
brew install gitOpen your terminal (Git Bash or PowerShell on Windows, Terminal on macOS) and run:
git --version
# Expected output: git version 2.x.x| Platform | Recommended Shell | Description |
|---|---|---|
| Windows | Git Bash (Top Pick) | Emulates a Unix environment inside Windows. Lets you use standard Linux commands (ls, pwd, mkdir, rm, cat) identically to macOS. |
| Windows | PowerShell / Windows Terminal | Modern native Windows shell. Fully supports git, but uses Windows-specific flags and cmdlets. |
| macOS | Terminal.app / iTerm2 | Built-in macOS shell powered by zsh. Fast, robust, and natively Unix. |
| Action | Windows (CMD / PowerShell) | macOS / Git Bash |
|---|---|---|
| List files | dir / Get-ChildItem |
ls -la |
| Print working folder | cd / pwd |
pwd |
| Clear screen | cls |
clear or Cmd + K |
| Copy / Paste | Ctrl + C / Ctrl + V |
Cmd + C / Cmd + V (macOS) or Ctrl + Shift + V (Git Bash) |
Warning
Windows uses Carriage Return + Line Feed (CRLF / \r\n) to represent new lines, while macOS and Linux use Line Feed (LF / \n).
If left unconfigured, Git may treat entire files as modified when team members collaborate across Windows and macOS!
Configure Git to handle line endings automatically according to your operating system:
git config --global core.autocrlf true(Converts LF to CRLF when checking out code, and converts CRLF back to LF when committing).
git config --global core.autocrlf input(Leaves LF unchanged on checkout, and ensures any accidental CRLF is converted to LF upon commit).
Run these commands on both Windows and macOS:
# Set your name and email (must match your GitHub account)
git config --global user.name "Your Full Name"
git config --global user.email "your.email@example.com"
# Set the modern default branch name
git config --global init.defaultBranch main
# Enable colored terminal output
git config --global color.ui auto- Windows: Git for Windows automatically configures Git Credential Manager to safely store your credentials in the Windows Credential Manager.
- macOS: Configure Git to use the native macOS Keychain:
git config --global credential.helper osxkeychain
Regardless of your operating system, Git manages code through four distinct states:
- Working Directory (Local Workspace): The active files on your local drive (
C:\...on Windows or/Users/...on macOS). - Staging Area (Index / Cache): The preparation area where you organize files before recording a snapshot.
- Local Repository (
.gitdatabase): The local history on your computer. Every commit is a permanent snapshot identified by a SHA-1 hash. - Remote Repository (GitHub / GitLab): The cloud-hosted copy shared with your team.
# In Git Bash:
cd /c/Users/YourUsername/Projects/my-app
git init
# Or in PowerShell:
cd C:\Users\YourUsername\Projects\my-app
git initcd ~/Projects/my-app
git initWorks identically on both Windows and macOS:
# Clone via HTTPS
git clone https://github.com/username/repository-name.git
# Or clone via SSH (if SSH keys are installed)
git clone git@github.com:username/repository-name.git
# Move into the cloned project folder
cd repository-nameThe day-to-day cycle is identical across platforms: check status, stage modifications, commit, and push.
Always check what has changed:
git statusChoose which files to include in your next commit:
# Stage a specific file
git add src/index.js
# Stage multiple specific files
git add src/index.js src/styles.css
# Stage all modified and untracked files
git add .Record a snapshot with a meaningful description:
git commit -m "feat: implement responsive navigation bar"Download team changes and upload your commits to GitHub:
# Download latest commits from GitHub
git pull origin main
# Upload your new commits to GitHub
git push origin mainIsolate your work from the production main branch to avoid breaking stable code:
# List local branches (* denotes your current active branch)
git branch
# Create and switch to a new feature branch (modern syntax)
git switch -c feature/user-profile
# (Classic equivalent: git checkout -b feature/user-profile)
# Switch back to the main branch
git switch main# 1. Switch to main
git switch main
# 2. Update main with remote changes
git pull origin main
# 3. Merge feature branch into main
git merge feature/user-profile
# 4. Push updated main to GitHub
git push origin main
# 5. Delete the feature branch once safely merged
git branch -d feature/user-profileNeed to switch branches or pull a hotfix without committing half-finished work? Use git stash:
# Shelve all current uncommitted modifications
git stash
# Shelve including any new untracked files
git stash -u
# Inspect your stash list
git stash list
# Re-apply stashed work and remove it from the stash stack
git stash pop
# Discard the most recent stash
git stash drop# Revert a single file back to the last commit
git restore src/app.js
# Revert ALL modified files in the working directory
git restore .git restore --staged src/app.jsgit add forgotten-file.js
git commit --amend -m "feat: complete user profile page"# Creates a new inverted commit without rewriting public history
git revert <commit-hash>A merge conflict happens when two teammates edit the exact same lines of the same file on different branches:
- Run
git statusto identify conflicting files underUnmerged paths. - Open the file in your code editor (VS Code highlights conflicts with buttons: Accept Current Change, Accept Incoming Change, or Accept Both).
- If editing manually, locate the conflict markers:
<<<<<<< HEAD (Current branch code) const API_BASE = "https://api.myapp.com"; ======= const API_BASE = "https://api-v2.myapp.com"; >>>>>>> feature/api-upgrade (Incoming branch code) - Choose the correct code and delete all markers (
<<<<<<<,=======,>>>>>>>). - Stage the resolved file:
git add src/config.js
- Finalize the merge commit:
git commit -m "fix: resolve API_BASE URL merge conflict"
When working in mixed Windows and macOS teams, always add platform-specific operating system artifacts to your .gitignore:
# ========================
# macOS System Junk
# ========================
.DS_Store
.AppleDouble
.LSOverride
._*
.Spotlight-V100
.Trashes
# ========================
# Windows System Junk
# ========================
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
Desktop.ini
$RECYCLE.BIN/
# ========================
# Development & Dependencies
# ========================
node_modules/
dist/
build/
.env
*.log
# ========================
# Code Editors
# ========================
.vscode/
.idea/
*.suo
*.userIn Git commands and .gitignore, always use forward slashes (/), even on Windows. Git understands forward slashes natively across all platforms (e.g., git add src/components/Header.jsx).