Sub topic 3
Stash options for real-world situations
This chapter assumes you know git stash push, git stash list, and git stash pop from the previous chapter. It also assumes you know the difference between tracked and untracked files. Here you will cover the options that matter once your work gets less tidy: new files, partial stashes, labels, inspection, and turning a stash into a branch.
Stash only skips what Git does not track
By default, git stash push saves changes to tracked files, both staged and unstaged. It leaves untracked files in place. This surprises people when they stash, switch branches, and find a new file still sitting in the working tree.
Use -u (--include-untracked) to stash new files too:
git stash push -u -m "wip with new files"Files that .gitignore excludes stay put. Add -a (--all) only if you also want ignored files stashed, such as build output. That is rarely what you want, because it can sweep up large generated directories.
Label your stashes
Without a message, Git labels a stash with the branch and last commit, such as WIP on main: 3f2a9c1 Fix typo. After a few of these, you can't tell them apart. Use -m every time:
git stash push -m "wip: login form validation"
git stash listThe list shows entries as stash@{0}, stash@{1}, and so on. The newest is always stash@{0}, and older entries shift up the numbers when you add another.
Stash only some files
Put a pathspec after -- to stash only matching paths. Everything else stays in your working tree:
git stash push -m "only the config change" -- config/settings.ymlFor finer control, git stash push -p (--patch) walks you through each change hunk by hunk and asks whether to stash it. This is useful when one file holds two unrelated edits.
Inspect before you restore
git stash list shows only labels. To see what a stash contains, use git stash show:
git stash show stash@{0}
git stash show -p stash@{0}The first form prints a summary of changed files. With -p, you get the full patch. Check this before applying an old stash, since the code around it may have changed.
Apply, pop, and drop
git stash apply restores a stash and keeps it in the list. git stash pop restores it and removes it, but only if it applied cleanly. If there is a conflict, Git keeps the stash so you do not lose it. Resolve the conflict, then run git stash drop yourself.
| Command | Restores changes | Removes the stash |
|---|---|---|
| `git stash apply` | Yes | No |
| `git stash pop` | Yes | Only if it applied cleanly |
| `git stash drop` | No | Yes |
Each takes an optional stash name, for example git stash apply stash@{2}.
Turn a stash into a branch
Sometimes a stash no longer applies cleanly because the branch moved on. git stash branch solves this. It creates a new branch at the commit where you made the stash, applies the stash there, and drops it if the apply succeeds:
git stash branch feature/login-rework stash@{0}Because the branch starts from the original commit, the changes apply without conflicts. You can then commit and merge or rebase as usual.
Common mistakes
- Forgetting
-u. New files stay behind, and the "clean" tree isn't clean. Check withgit statusafter stashing. - Relying on unlabeled stashes. Use
-mso you can find the right one later. - Popping onto the wrong branch. A stash is not tied to a branch.
popapplies it wherever you are, so check your branch first. - Treating the stash as long-term storage. Stashes are easy to forget and hard to find after weeks. If the work matters, commit it to a branch.
Exercise
You have a modified tracked file app.py, a modified tracked file config/settings.yml, and a new untracked file notes.txt. Write one command that stashes only the config change with the message "only the config change". Then say what happens to app.py and notes.txt.
Show answer
Run git stash push -m "only the config change" -- config/settings.yml. Both app.py and notes.txt stay in the working tree unchanged. The pathspec limits the stash to the one file, and notes.txt is untracked, so it would be stashed only if you also passed -u and it matched the pathspec.
Exercise
You run git stash pop and Git reports a conflict. Is the stash still in git stash list? What should you do next?
Show answer
Yes, the stash is kept because it did not apply cleanly. Resolve the conflicts, stage the files, and then run git stash drop to remove the stash once you no longer need it.