Git has hundreds of commands and options, but 90% of your daily work uses maybe 20. This cheat sheet covers the commands you'll actually reach for — organized by workflow, with real examples and the gotchas nobody warns you about.
Setup & Configuration
# Set your identity (required before first commit)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Set default branch name
git config --global init.defaultBranch main
# Set VS Code as default editor
git config --global core.editor "code --wait"
# Enable auto-coloring
git config --global color.ui auto
# See all your settings
git config --list --show-origin
Creating & Cloning Repos
# Initialize a new repo in current directory
git init
# Clone a remote repo
git clone https://github.com/user/repo.git
# Clone into a specific folder
git clone https://github.com/user/repo.git my-folder
# Clone only the most recent commit (faster for huge repos)
git clone --depth 1 https://github.com/user/repo.git
Daily Workflow
# Check what's changed
git status # Shows modified/staged/untracked files
git diff # Shows unstaged changes (line by line)
git diff --staged # Shows staged changes (what will be committed)
# Stage files
git add file.js # Stage specific file
git add . # Stage everything in current directory
git add -p # Stage interactively (hunk by hunk)
# Commit
git commit -m "Add login feature"
git commit -am "Fix typo" # Stage tracked files + commit in one step
# View history
git log --oneline # Compact log (one line per commit)
git log --graph --oneline # Visual branch graph
git log -5 # Last 5 commits
git log --author="Rahul" # Commits by specific author
git log -- file.js # History of a specific file
💡 Pro Tip: Use
git add -p to review and stage changes hunk by hunk. It prevents accidentally committing debug code or unrelated changes.
Branching
# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create & switch
git branch feature/login # Create branch
git checkout feature/login # Switch to it
git checkout -b feature/login # Create + switch (shortcut)
git switch feature/login # Modern alternative to checkout
git switch -c feature/login # Create + switch (modern)
# Rename current branch
git branch -m new-name
# Delete branch
git branch -d feature/login # Safe delete (only if merged)
git branch -D feature/login # Force delete (even if unmerged)
Merging
# Merge feature branch into main
git checkout main
git merge feature/login
# Merge with a merge commit (even if fast-forward is possible)
git merge --no-ff feature/login
# Cancel a merge during conflicts
git merge --abort
Resolving merge conflicts
# 1. Git marks conflicts in files:
<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> feature/login
# 2. Edit the file, keep what you want, remove markers
# 3. Stage the resolved file
git add resolved-file.js
# 4. Complete the merge
git commit
Rebasing
# Rebase current branch onto main (linear history)
git rebase main
# Interactive rebase — squash, reword, reorder last 3 commits
git rebase -i HEAD~3
# Cancel a rebase
git rebase --abort
# Continue after fixing conflicts
git rebase --continue
⚠️ Golden Rule: Never rebase commits that have been pushed and shared with others. Rebasing rewrites history — only use it on local/feature branches.
Stashing
# Save uncommitted changes temporarily
git stash
# Save with a descriptive message
git stash save "WIP: login form validation"
# Include untracked files
git stash -u
# List all stashes
git stash list
# Apply most recent stash (keep it in stash list)
git stash apply
# Apply and remove from stash list
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 Things
# Unstage a file (keep changes in working directory)
git restore --staged file.js
# Discard changes in working directory
git restore file.js
# Amend the last commit message
git commit --amend -m "New message"
# Add forgotten files to last commit
git add forgot-this.js
git commit --amend --no-edit
# Undo last commit but keep changes staged
git reset --soft HEAD~1
# Undo last commit and unstage changes
git reset HEAD~1 # Default: --mixed
# Undo last commit and DISCARD all changes (DANGEROUS)
git reset --hard HEAD~1
# Create a new commit that undoes a specific commit (safe for shared history)
git revert abc1234
Working with Remotes
# View remotes
git remote -v
# Add a remote
git remote add origin https://github.com/user/repo.git
# Fetch updates (doesn't merge)
git fetch origin
# Pull (fetch + merge)
git pull origin main
# Pull with rebase (cleaner history)
git pull --rebase origin main
# Push
git push origin main
git push -u origin feature/login # First push + set upstream
# Delete remote branch
git push origin --delete feature/login
Cherry-Picking
# Apply a specific commit to current branch
git cherry-pick abc1234
# Cherry-pick without committing (stage changes only)
git cherry-pick --no-commit abc1234
# Cherry-pick a range of commits
git cherry-pick abc1234..def5678
Inspecting & Debugging
# Show details of a specific commit
git show abc1234
# Who changed each line? (blame)
git blame file.js
# Find which commit introduced a bug (binary search)
git bisect start
git bisect bad # Current commit is broken
git bisect good abc1234 # This commit was working
# Git checks out middle commit — test it, then:
git bisect good # or git bisect bad
# Repeat until Git finds the first bad commit
git bisect reset # Return to original state
# Search commit messages
git log --grep="login"
# Search code changes
git log -S "functionName" # Find commits that added/removed this string
Tags
# Create a tag
git tag v1.0.0
git tag -a v1.0.0 -m "Release 1.0.0" # Annotated tag (recommended)
# List tags
git tag
# Push tags to remote
git push origin v1.0.0 # Single tag
git push origin --tags # All tags
# Delete a tag
git tag -d v1.0.0 # Local
git push origin --delete v1.0.0 # Remote
Quick Reference Table
TASK COMMAND
─────────────────────── ──────────────────────────────
See what changed git status / git diff
Stage changes git add . / git add -p
Commit git commit -m "message"
New branch git switch -c branch-name
Switch branch git switch branch-name
Merge git merge branch-name
Pull latest git pull --rebase origin main
Push changes git push origin branch-name
Undo last commit git reset --soft HEAD~1
Stash work-in-progress git stash -u
View log git log --oneline --graph
Find who changed a line git blame file.js