Skip to content
$ lynuxpro
← Git

LESSON 10 OF 21 · INTERMEDIATE · 15 MIN

Stashing and cleaning

HOW IT WORKSStash: put work aside

Half-done workdirty treestash@{0}saved changesClean treeswitch freelyHotfix branchurgent fixWork restoredgit stash pop
1/4

An urgent bug arrives while you are in the middle of a change that is not ready to commit.

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"
SHELLbashutf-8 · 8L

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
SHELLbashutf-8 · 5L
 M cart.pySaved working directory and index state On main: wip: cart discountsdef cart():    return []
OUTPUTterminalutf-8 · 4L

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 -p
SHELLbashutf-8 · 2L
stash@{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 items
OUTPUTterminalutf-8 · 11L

Stashes form a stack: the newest is stash@{0}, the one before stash@{1}, and so on.

Bring the work back:

git stash popgit stash list
SHELLbashutf-8 · 2L
On 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)
OUTPUTterminalutf-8 · 8L
CommandEffect
git stash popapply the newest stash and remove it from the list
git stash applyapply 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 cleardelete all entries (cannot be undone)
git stash branch new-branchcreate 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 popls
SHELLbashutf-8 · 5L
Saved 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.py
OUTPUTterminalutf-8 · 15L

The 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 off
SHELLbashutf-8 · 6L

Stashes 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 -s
SHELLbashutf-8 · 6L
Would remove build/Would remove discount.pyWould remove notes.tmpRemoving build/Removing discount.pyRemoving notes.tmp M cart.py
OUTPUTterminalutf-8 · 7L
OptionMeaning
-ndry run: show what would be removed
-factually remove (required)
-dinclude untracked folders
-xalso remove ignored files (build output, node_modules)
-iinteractive: 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 clear by accident; it deletes every entry.
  • git clean -fdx without a dry run, deleting ignored local config and caches.

Practice

  1. Modify a file, stash it with a message, check that the tree is clean, then pop it back.
  2. Create a new untracked file and stash it with -u; confirm it disappears and comes back.
  3. Make two stashes and apply the older one with git stash apply stash@{1}.
  4. 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)
OUTPUTterminalutf-8 · 11L

Key takeaways

  • git stash saves unfinished work and gives you a clean tree; pop brings it back.
  • Use -m for messages and -u to include untracked files.
  • Stashes are local and short-term; commit work that must last.
  • git clean removes untracked files; always run -n first, and add -x only when you mean it.