Appearance
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~1Status & 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..def456Staging & 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 --amendBranching
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-featureMerging & 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 abc123Remotes
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-leaseHistory
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.pyStashing
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 clearUndoing 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 abc123Tags
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.0Common Patterns
Undo a pushed commit safely
bash
# Create a revert commit (safe for shared branches)
git revert HEAD
git push origin mainPull in changes without a merge commit
bash
git pull --rebase origin mainSquash 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 HEADClean 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 -fdQuick Reference
| Task | Command |
|---|---|
| Show status | git status |
| Show unstaged diff | git diff |
| Stage all | git add . |
| Stage interactively | git add -p |
| Commit | git commit -m "<message>" |
| Amend last commit | git commit --amend |
| Create & switch branch | git switch -c <branch> |
| Switch branch | git switch <branch> |
| Merge branch | git merge <branch> |
| Rebase onto main | git rebase main |
| Pull with rebase | git pull --rebase |
| Push branch | git push origin <branch> |
| View log | git log --oneline --graph |
| Stash changes | git stash |
| Pop stash | git stash pop |
| Undo last commit (soft) | git reset --soft HEAD~1 |
| Discard file changes | git restore <file> |
| Safe revert | git revert HEAD |
Related
- Git Setup — Full Git configuration with aliases, hooks, and templates
- Git Branch Hygiene — Workflow guide for keeping branches clean