git stash lets you set aside uncommitted work-in-progress changes on demand, giving you a clean working directory to handle an urgent task, with the ability to restore your exact state afterward.
Key Points: • git stash saves both staged and unstaged modifications and resets the working directory to match the last commit. • The stash is stored as a special commit-like object on a stack, so you can create several stashes before returning to any of them. • git stash pop reapplies the most recent stash and removes it from the stack; git stash apply does the same but keeps the entry for reuse. • git stash list shows all pending stashes if you need to manage more than one at a time. • Stashing avoids creating throwaway "WIP" commits just to switch context, keeping history clean.
Example: While halfway through a refactor, an urgent production bug needs fixing; running git stash clears the working directory, the bug gets fixed and committed on its own branch, and git stash pop afterward brings the refactor's uncommitted changes right back.
Code Example:
git stash
# fix the urgent bug, commit, push
git stash popInterview Tip: A concise interview answer is:
"I'd run git stash to set aside my uncommitted changes and get a clean working directory, handle the urgent bug fix, and then run git stash pop to bring my original work back exactly as it was. It avoids committing half-finished code just to switch tasks."