Search Knowledge

© 2026 LIBREUNI PROJECT

Stashing and Cleaning

The Interrupt Driven Workflow

You are deep in the zone, coding a complex feature. Suddenly, a critical bug is reported in production. You need to switch to the main branch to fix it, but your current working directory is a mess of half-finished changes.

You can’t commit, because the code doesn’t compile. You can’t just switch branches, because Git will forbid it if your changes conflict with the destination branch.

Enter git stash.

Stashing Changes

Stashing takes the dirty state of your working directory (modified tracked files and staged changes) and saves it on a stack of unfinished changes that you can reapply at any time.

git stash

Your working directory is now clean (matching the HEAD commit). You can safely switch branches, fix the bug, and commit.

Naming Stashes

By default, stashes are named “WIP on branch…“. To make them easier to find later:

git stash push -m "Experimenting with new login logic"

Retrieving Stashes

Once you are done with the bug fix and back on your feature branch, you want your changes back.

Pop

git stash pop removes the latest stash from the stack and applies it to your working directory.

git stash pop

Apply

If you want to apply the stash but keep it in the stack (e.g., to apply it to multiple branches):

git stash apply

Managing the Stack

You can have multiple stashes.

git stash list

Output:

stash@{0}: On feature: Experimenting with new login logic
stash@{1}: WIP on main: 4d3e2f1 Fix typo

To apply a specific stash:

git stash apply stash@{1}

To delete a stash:

git stash drop stash@{0}

To clear all stashes:

git stash clear

Stashing Untracked Files

By default, git stash only saves tracked files. If you have created new files (untracked), they will remain in your working directory. To stash them as well:

git stash -u

(or --include-untracked)

Cleaning the Workplace

Sometimes you just want to destroy untracked files. Maybe your build script generated a thousand .o files and you want to start fresh.

git clean removes untracked files from the working directory.

Dry Run: Always do this first to see what would be deleted.

git clean -n

Force Clean: Actually delete the files.

git clean -f

Directories too:

git clean -fd

Ignored files: To remove ignored files as well (e.g., node_modules):

git clean -fdX
git
1# Simulate a dirty state
2git init
3echo "base" > file.txt
4git add file.txt
5git commit -m "Base"
6echo "work in progress" >> file.txt
7 
8# Stash it
9git stash
10cat file.txt # Should be just "base"
11 
12# Bring it back
13git stash pop
14cat file.txt # Should have "work in progress"

What is the difference between 'git stash pop' and 'git stash apply'?

Listing Stashes

# You want to see the list of all saved stashes to find an old experiment.
git stash 
Previous Module Undoing Changes