You're halfway through writing a test. Your lead says "Hey, drop what you're doing — there's a critical locator fix needed on main." You can't commit half-written code. You can't switch branches with uncommitted changes. What do you do?
git stash. It saves your work-in-progress without committing, gives you a clean workspace, and lets you come back to it later. Think of it like putting your plate in the fridge — you'll eat it later.
# You have uncommitted changes and need to switch branches
# Save your changes to the stash
git stash
# Saved working directory and index state WIP on feature/login-tests
# Your working directory is now clean — you can switch branches
git checkout main
# ... do the urgent fix, commit, push ...
# Go back to your branch
git checkout feature/login-tests
# Bring back your stashed changes
git stash pop
# Your half-written code is back exactly where you left it# Stash with a message (when you have multiple stashes)
git stash save "WIP: login test - need to add assertions"
# List all stashes
git stash list
# stash@{0}: On feature/login-tests: WIP: login test - need to add assertions
# stash@{1}: WIP on feature/cart-tests: abc123 message
# Apply a specific stash (without removing it from the list)
git stash apply stash@{1}
# Drop a stash you don't need anymore
git stash drop stash@{0}
# Clear ALL stashes (careful)
git stash clear| Command | What It Does | When to Use |
|---|---|---|
| git log --oneline --graph | Visual branch history | See how branches merged |
| git blame LoginPage.java | Shows who wrote each line | "Who changed this locator?" |
| git show abc1234 | Shows a specific commit's changes | Review what a commit actually did |
| git diff main..feature/login-tests | Diff between two branches | See all changes before creating PR |
| git reset --soft HEAD~1 | Undo last commit, keep changes staged | "I committed too early, let me fix" |
| git cherry-pick abc1234 | Copy a specific commit to current branch | Grab one fix without merging everything |
# Who changed this file? (shows line-by-line history)
git blame LoginPage.java
# Shows: commit hash | author | date | line content
# Undo last commit but keep the changes
git reset --soft HEAD~1
# Now the changes are staged again — fix and recommit
# See the full diff of what your branch changed vs main
git diff main..feature/login-testsgit blame is your best friend when debugging. A test was passing last week, now it's failing. Run git blame on the test file, find who changed it, look at their commit. 90% of the time, that's where the bug is.
git reset --hard HEAD~1 deletes the last commit AND the changes. Use --soft if you want to keep the changes. --hard is the nuclear option — use it only when you really want to throw away work.
Key Point: git stash saves your work-in-progress without committing. git blame shows who changed what. These are your daily productivity tools.