Learning Objectives
By the end of this lesson you will be able to:
- Describe why an interruption is awkward when your work is half-finished
- List the four ways out and the real cost of each
- Choose the right one for a given situation
- Recognise when a stash is not the right answer
The Situation
It is 14:20. You are two-thirds of the way through rewriting a flaky fixture: six files changed, nothing runs yet, and the whole thing exists only in your working directory.
Then a message arrives. Payments are double-charging in production. A fix is on hotfix/PAY-4417 and someone needs it verified in the next twenty minutes.
You need to be on a different branch, with a clean working tree, right now. And you need everything you have done since lunch to still exist when you come back.
Why It's Awkward
Recall from Module 1: uncommitted changes travel with you when you switch branches. Sometimes that is convenient. Here it is not — you would carry six half-finished files onto the hotfix branch, and:
- The hotfix build might not compile with your fixture rewrite half-applied
- If your changes touch the same files as the hotfix, Git will refuse to switch at all
- Even if it works, you are now testing a hotfix in a working tree contaminated by unrelated changes, and any failure is ambiguous
You need the changes off the working tree, and safely somewhere.
The Four Options
1. Commit it to your branch
git add .
git commit -m "wip: fixture rewrite, incomplete"
git switch hotfix/PAY-4417
Works. Nothing can be lost — it is a real commit in real history.
Cost: a commit that doesn't build sits in your branch's history. If you push it, everyone sees it. Many teams find this perfectly acceptable on a personal branch and clean it up later; others don't. It is honest and safe, and it is the right answer when the interruption might last days.
2. Stash it
git stash push -m "fixture rewrite, 2/3 done"
git switch hotfix/PAY-4417
Works, and leaves no trace in history. Your working tree is instantly clean, the changes are safe on the stash stack, and one command brings them back.
Cost: the stash is local-only and easy to forget. It is the right answer for exactly this scenario — a short interruption, work you want back within hours.
3. Copy the files somewhere
cp -r tests/ ~/Desktop/backup-tests/
Please don't. It loses which branch the work belonged to, loses the relationship to the commits it was based on, and reliably produces a folder called backup-tests-final-2 that nobody dares delete for a year. Every problem it solves, Git solves better.
4. Throw the work away
git restore .
Sometimes correct! If you had been experimenting for ten minutes and learned what you needed, discarding is genuinely the cleanest option. It is only a mistake when made by accident.
Choosing
| Situation | Use |
|---|---|
| Short interruption, want the work back soon | Stash |
| Interruption may last days, or work is nearly done | Commit to your branch |
| Work is exploratory and no longer needed | Discard |
| Work needs to be handed to a colleague | Commit and push |
That last row is worth emphasising, because it is the stash's hard limit: a stash never leaves your machine. There is no git stash push origin. If someone else needs your unfinished work — you're going on holiday, you're off sick, another tester is picking it up — it must become a commit on a pushed branch. A stash cannot do it.
The Whole Flow
git stash push -m "fixture rewrite, 2/3 done" # pocket the work
git switch main
git pull # get current code
git switch hotfix/PAY-4417 # onto the fix
# ... verify the hotfix, report the result ...
git switch test/fixture-rewrite # back to your branch
git stash pop # and your work returns
Six commands, and the interruption cost you nothing.
Pro Tip: Before stashing, run
git statusand read the file list. Knowing you stashed six specific files is what makes it obvious, two hours later, whether the stash came back complete.
Key Takeaways
- Uncommitted changes follow you across branches, which is exactly wrong during an interruption
- Four options: commit, stash, copy files, discard — three of them are legitimate
- Stash fits short interruptions where you want the work back within hours
- Committing fits long interruptions and anything nearly finished
- Never copy files out of the repository as a backup strategy
- A stash never leaves your machine — work anyone else needs must become a pushed commit