Git Cheatsheet — Commands I Actually Use
Updated Originally
A personal reference of git commands I find myself looking up too often.
Undo & Fix
# Undo last commit but keep changes staged
git reset --soft HEAD~1
# Undo last commit and unstage changes
git reset HEAD~1
# Discard all local changes (destructive!)
git reset --hard HEAD
# Fix the last commit message
git commit --amend -m "Better message"
# Remove a file from last commit
git reset HEAD~ path/to/file
git commit --amend --no-edit
Stash
# Stash with a name
git stash push -m "work in progress: auth flow"
# List stashes
git stash list
# Apply and remove specific stash
git stash pop stash@{2}
# Apply without removing
git stash apply stash@{2}
# Stash only staged changes
git stash push --staged
Branches
# Create and switch in one command
git switch -c feature/new-thing
# Rename current branch
git branch -m new-name
# Delete remote branch
git push origin --delete branch-name
# See which branches are merged
git branch --merged main
History & Diff
# Prettier log
git log --oneline --graph --decorate
# Show changes in a commit
git show abc1234
# Search commit messages
git log --grep="fix: auth"
# Find when a line was introduced
git log -S "searchTerm" --source --all
# Diff staged changes
git diff --staged
Rebase
# Interactive rebase last 3 commits
git rebase -i HEAD~3
# Rebase onto main
git rebase main
# Abort a rebase
git rebase --abort
Cherry-pick
# Apply a specific commit to current branch
git cherry-pick abc1234
# Cherry-pick without committing
git cherry-pick --no-commit abc1234
Aliases (in ~/.gitconfig)
[alias]
st = status
co = checkout
br = branch
lg = log --oneline --graph --decorate --all
unstage = reset HEAD --
last = log -1 HEAD