What is git stash and when would you use it?
Quick Answer
git stash temporarily saves your uncommitted changes (both staged and unstaged) onto a stack, giving you a clean working directory. You can later restore them with git stash pop. It's commonly used when you need to switch branches urgently without committing half-done work.
Detailed Answer
Think of git stash like a desk drawer where you quickly sweep all your papers into when someone walks in with an urgent task. Your desk is now clean and you can work on the urgent task. When you're done, you pull the papers back out of the drawer and continue where you left off. The drawer even remembers the order you stacked things if you use it multiple times.
The git stash command takes your uncommitted modifications (tracked files that are modified, staged changes, and optionally untracked files) and saves them on a stack of stash entries. Each stash is stored as a special commit object that's not part of any branch. Your working directory and index are then reverted to match the HEAD commit. You can create multiple stashes, list them with git stash list, and apply any specific stash by its index. The most recent stash is always stash@{0}.
Internally, git stash creates two commit objects: one representing the state of the index (staging area) and one representing the state of the working directory. These commits reference the HEAD commit as their parent but are stored in a special reflog at .git/refs/stash rather than on any branch. When you run git stash pop, Git applies the stash as a merge against your current working directory and, if successful, drops the stash entry. If applying the stash results in conflicts, Git keeps the stash entry so you can resolve conflicts and try again without losing the stashed changes.
In production workflows, stashing is invaluable for context switching. A common scenario: you're deep in implementing a feature on feature/recommendation-engine when a critical production bug alert fires. You git stash your in-progress work, switch to main, create a hotfix branch, fix the bug, deploy, and then switch back to your feature branch and git stash pop. Without stash, you'd either need to commit incomplete work (polluting the history) or risk losing changes. Stashing is also useful before running git pull --rebase when you have local modifications, and before running complex refactoring operations where you want a safety net.
A common gotcha: git stash by default does not include untracked files (files Git has never seen). Use git stash -u or git stash --include-untracked to include them. Also, stash entries can pile up if you forget about them. Periodically run git stash list to check, and git stash drop stash@{n} to clean up old entries. An important trap: git stash pop removes the stash entry after applying it. If something goes wrong during apply, use git stash apply instead, which keeps the stash entry intact as a safety measure. Finally, don't use stash as a long-term storage mechanism. If you need to save work for more than a few hours, create a proper branch instead.