You are halfway through a change when something urgent comes up: a production bug, a reviewer asking for a quick fix on another branch, or a git pull that refuses to run because of your local edits. Your work is not ready to commit. git stash puts it safely aside and gives you a clean working tree; later you bring it back exactly as it was. This lesson also covers git clean, which removes untracked files such as build output.
In simple words
- Stash: a temporary shelf for unfinished changes, so you can switch tasks with a clean working tree.
- git stash pop: brings stashed changes back and removes them from the shelf.
- git clean: deletes untracked files. Use -n first to see what would go.
Set up
git config --global user.name "Leela Kumar"mkdir stash-labcd stash-labgit initprintf 'def cart():\n return []\n' > cart.pyprintf '# Shop\n' > README.mdgit add .git commit -m "Add cart and README"Stash your work
Start a change that is not ready:
printf 'def cart():\n items = []\n # TODO: discounts\n return items\n' > cart.pygit status -sgit stash push -m "wip: cart discounts"git status -scat cart.py M cart.pySaved working directory and index state On main: wip: cart discountsdef cart(): return []The stash saved both your staged and unstaged changes to tracked files, and reset the files to the last commit. You can now switch branches, pull or fix something else.
Always give stashes a message with -m. A list of five entries called "WIP on main" is useless a week later.
List, inspect and bring back
git stash listgit stash show -pstash@{0}: On main: wip: cart discountsdiff --git a/cart.py b/cart.pyindex 1d7a4e2..8b3f9c0 100644--- a/cart.py+++ b/cart.py@@ -1,2 +1,4 @@ def cart():- return []+ items = []+ # TODO: discounts+ return itemsStashes form a stack: the newest is stash@{0}, the one before stash@{1}, and so on.
Bring the work back:
git stash popgit stash listOn branch mainChanges not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: cart.pyno changes added to commit (use "git add" and/or "git commit -a")Dropped refs/stash@{0} (4f2c9e1)| Command | Effect |
|---|---|
git stash pop | apply the newest stash and remove it from the list |
git stash apply | apply it but keep it in the list (useful on several branches) |
git stash apply stash@{2} | apply a specific entry |
git stash drop stash@{1} | delete one entry |
git stash clear | delete all entries (cannot be undone) |
git stash branch new-branch | create a branch from where the stash was made and apply it there |
If applying a stash conflicts with changes made since, Git marks the conflict like a merge and keeps the stash entry, so nothing is lost. Resolve it, then git stash drop.
Untracked files
By default, git stash only saves changes to tracked files. New files stay in your folder. Include them with -u:
printf 'def discount(x):\n return x * 0.9\n' > discount.pygit stash push -u -m "wip: discounts with new file"lsgit stash poplsSaved working directory and index state On main: wip: discounts with new fileREADME.md cart.pyOn branch mainChanges not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: cart.pyUntracked files: (use "git add <file>..." to include in what will be committed) discount.pyno changes added to commit (use "git add" and/or "git commit -a")Dropped refs/stash@{0} (9a1b7c2)README.md cart.py discount.pyThe typical interruption
git stash push -m "wip: discounts" # put work asidegit switch main && git pull # get the latest codegit switch -c fix/checkout-timeout # fix the urgent bug# … fix, commit, push, open a merge request …git switch feature/discounts # back to your featuregit stash pop # continue where you left offStashes are local only; they are never pushed. Treat them as a short-term shelf, not long-term storage. If work needs to survive more than a day, commit it on a branch (a "WIP" commit you clean up later with interactive rebase is fine).
git clean: remove untracked files
Build tools, tests and experiments leave untracked files behind. git clean deletes them, which is useful for a guaranteed fresh state. Because deleted untracked files cannot be recovered by Git, it refuses to run without -f, and you should always do a dry run first with -n:
mkdir -p buildprintf 'artifact\n' > build/app.binprintf 'scratch\n' > notes.tmpgit clean -n -dgit clean -f -dgit status -sWould remove build/Would remove discount.pyWould remove notes.tmpRemoving build/Removing discount.pyRemoving notes.tmp M cart.py| Option | Meaning |
|---|---|
-n | dry run: show what would be removed |
-f | actually remove (required) |
-d | include untracked folders |
-x | also remove ignored files (build output, node_modules) |
-i | interactive: choose file by file |
git clean -fdx resets a project to exactly what is in Git, a common first step in CI jobs and when a build behaves strangely. Remember that -x also deletes ignored files you may care about, like a local .env.
Real-world scenario: the urgent review fix
Priya is deep in a refactor with twelve modified files when her lead asks for a one-line change on a merge request that is about to be released. She runs git stash push -m "refactor: payments client", switches to the release branch, makes and pushes the fix, then switches back and runs git stash pop. The whole detour takes three minutes and none of the half-finished refactor leaks into the release.
Common mistakes
- Forgetting
-u, then being surprised that new files stayed behind (or followed you to another branch). - Stashes without messages, which become impossible to tell apart.
- Using stash as long-term storage; old stashes get forgotten or cleared. Commit on a branch instead.
git stash clearby accident; it deletes every entry.git clean -fdxwithout a dry run, deleting ignored local config and caches.
Practice
- Modify a file, stash it with a message, check that the tree is clean, then
popit back. - Create a new untracked file and stash it with
-u; confirm it disappears and comes back. - Make two stashes and apply the older one with
git stash apply stash@{1}. - Create some untracked files and folders, preview with
git clean -n -d, then remove them.
Cheat sheet
git stash push -m "msg" save tracked changesgit stash push -u -m "msg" include untracked filesgit stash list list entriesgit stash show [-p] [stash@{n}] summary / full diffgit stash pop [stash@{n}] apply and removegit stash apply [stash@{n}] apply and keepgit stash drop stash@{n} delete onegit stash branch name new branch from a stashgit clean -n -d dry rungit clean -f -d remove untracked files and foldersgit clean -fdx also ignored files (fresh checkout)Key takeaways
git stashsaves unfinished work and gives you a clean tree;popbrings it back.- Use
-mfor messages and-uto include untracked files. - Stashes are local and short-term; commit work that must last.
git cleanremoves untracked files; always run-nfirst, and add-xonly when you mean it.