Skip to content

Git Cheatsheet

Published: March 7, 2026 · Last edited: March 7, 2026

A practical reference for Git commands you need mid-task — staging, branching, history, remotes, and recovery.

The 30-Second Version

bash
# See what changed
git status
git diff

# Stage and commit
git add -p
git commit -m "feat: add login page"

# Push to remote
git push origin main

# Pull latest changes
git pull --rebase

# Undo last commit (keep changes staged)
git reset --soft HEAD~1

Status & Diff

bash
# Show working tree status
git status

# Show unstaged changes
git diff

# Show staged changes (ready to commit)
git diff --staged

# Show changes in a specific file
git diff path/to/file.py

# Show what changed between two commits
git diff abc123..def456

Staging & Committing

bash
# Stage all changes
git add .

# Stage specific file
git add path/to/file.py

# Stage changes interactively (hunk by hunk)
git add -p

# Unstage a file (keep changes in working tree)
git restore --staged path/to/file.py

# Commit with message
git commit -m "feat: add user authentication"

# Stage all tracked files and commit in one step
git commit -am "fix: correct off-by-one error"

# Amend the last commit (message or staged changes)
git commit --amend

Branching

bash
# List local branches
git branch

# List all branches (including remote-tracking)
git branch -a

# Create and switch to a new branch
git switch -c feature/my-feature

# Switch to an existing branch
git switch main

# Rename the current branch
git branch -m new-name

# Delete a branch (safe — refuses if unmerged)
git branch -d feature/my-feature

# Force delete a branch
git branch -D feature/my-feature

Merging & Rebasing

bash
# Merge a branch into the current branch
git merge feature/my-feature

# Merge without fast-forward (always creates a merge commit)
git merge --no-ff feature/my-feature

# Rebase current branch onto main
git rebase main

# Interactive rebase — edit, squash, reorder last 3 commits
git rebase -i HEAD~3

# Abort an in-progress rebase
git rebase --abort

# Continue after resolving conflicts
git rebase --continue

# Cherry-pick a single commit onto the current branch
git cherry-pick abc123

Remotes

bash
# List remotes
git remote -v

# Add a remote
git remote add origin git@github.com:user/repo.git

# Fetch changes (doesn't merge)
git fetch origin

# Pull and rebase instead of merge
git pull --rebase

# Push current branch
git push origin feature/my-feature

# Push and set upstream tracking in one step
git push -u origin feature/my-feature

# Delete a remote branch
git push origin --delete feature/my-feature

# Force push (use with caution on shared branches)
git push --force-with-lease

History

bash
# Show commit log
git log

# Compact one-line log
git log --oneline

# Log with branch graph
git log --oneline --graph --all

# Show changes introduced by a commit
git show abc123

# Search commit messages
git log --grep="authentication"

# Find commits that changed a specific file
git log -- path/to/file.py

# Show who last changed each line of a file
git blame path/to/file.py

Stashing

bash
# Stash current changes
git stash

# Stash with a descriptive name
git stash push -m "WIP: refactor auth"

# List stashes
git stash list

# Apply the most recent stash (keeps it in the list)
git stash apply

# Apply and drop the most recent stash
git stash pop

# Apply a specific stash
git stash apply stash@{2}

# Drop a specific stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

Undoing Changes

bash
# Discard unstaged changes in a file
git restore path/to/file.py

# Discard all unstaged changes
git restore .

# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last commit, keep changes unstaged
git reset HEAD~1

# Undo last commit, discard all changes (destructive)
git reset --hard HEAD~1

# Revert a commit by creating a new "undo" commit
git revert abc123

Tags

bash
# List tags
git tag

# Create a lightweight tag
git tag v1.0.0

# Create an annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"

# Tag a specific commit
git tag -a v1.0.0 abc123 -m "Release version 1.0.0"

# Push a tag to remote
git push origin v1.0.0

# Push all tags
git push origin --tags

# Delete a local tag
git tag -d v1.0.0

# Delete a remote tag
git push origin --delete v1.0.0

Common Patterns

Undo a pushed commit safely

bash
# Create a revert commit (safe for shared branches)
git revert HEAD
git push origin main

Pull in changes without a merge commit

bash
git pull --rebase origin main

Squash all commits on a feature branch before merging

bash
# Interactively rebase onto main
git rebase -i main
# Mark all but the first commit as "squash" or "s"

Find which commit introduced a bug

bash
git bisect start
git bisect bad                # current commit is broken
git bisect good v1.2.0        # last known good tag
# Git checks out a midpoint — test it, then:
git bisect good               # or: git bisect bad
# Repeat until Git identifies the culprit commit
git bisect reset              # return to HEAD

Clean up untracked files

bash
# Preview what would be deleted
git clean -n

# Delete untracked files
git clean -f

# Delete untracked files and directories
git clean -fd

Quick Reference

TaskCommand
Show statusgit status
Show unstaged diffgit diff
Stage allgit add .
Stage interactivelygit add -p
Commitgit commit -m "<message>"
Amend last commitgit commit --amend
Create & switch branchgit switch -c <branch>
Switch branchgit switch <branch>
Merge branchgit merge <branch>
Rebase onto maingit rebase main
Pull with rebasegit pull --rebase
Push branchgit push origin <branch>
View loggit log --oneline --graph
Stash changesgit stash
Pop stashgit stash pop
Undo last commit (soft)git reset --soft HEAD~1
Discard file changesgit restore <file>
Safe revertgit revert HEAD
  • Git Setup — Full Git configuration with aliases, hooks, and templates
  • Git Branch Hygiene — Workflow guide for keeping branches clean