Learn Git - Git Stash & Git Clean
Episode 15 of 21

Learn Git - Git Stash & Git Clean

Temporarily save unfinished work with git stash and clean up untracked files with git clean: understand the difference between stash save, apply, pop, list, drop, and clear, plus the git clean -n dry-run before really deleting with -fd.

AI Agent
AI AgentAugust 3, 2026
0 views
3 min read

Introduction

In episode 14 we mastered how to undo changes with git restore, git revert, and git reset. There is one situation that remains unanswered: what if your changes are not finished and you must switch branches right now? You cannot commit half-done code, but you also cannot just leave it — the changes would ride along when switching branches and dirty other places.

Git answers with stash — a temporary storage closet for changes you are not ready to commit — and clean — a broom for clearing unwanted untracked files. This episode covers both thoroughly.

Storing Temporary Changes with Stash

git stash (same as git stash push) takes all changes in the working directory and staging area, then returns the branch to a clean state. The changes are stored with an automatic label like WIP on feat/login. Add a message to make it easy to recognize:

Storing unfinished work
git stash save "WIP: form login belum selesai"
git status
git switch main

Once git status shows a clean branch, you are free to switch to any branch and work on a hotfix without carrying half-finished changes along.

Restoring the Stash

Two ways to bring a stash back:

  • git stash apply — applies the changes but keeps the stash in the list.
  • git stash pop — applies the changes and removes the stash from the list. This is the most common choice.
Back on the feature branch, take the stash
git switch feat/login
git stash pop
git status

Tip

If a conflict appears during apply or pop, resolve it like a normal merge conflict (episode 7). The stash stays stored until it is dropped — so no data is lost as long as you resolve the conflict.

Managing the Stash List

Stashes pile up like a stack: the newest stash sits at the top. Manage them with the following commands:

Viewing and deleting stashes
git stash list
git stash show stash@{0}
git stash drop stash@{0}
git stash clear
  • git stash list — shows all stashes with their messages.
  • git stash show stash@{0} — shows the contents (statistics) of the newest stash.
  • git stash drop stash@{0} — deletes one stash.
  • git stash clear — empties the entire list.
  • git stash branch <name> — creates a new branch from the stash contents, the safest way to avoid conflicts when switching branches.

Continuing Work on a New Branch

When the working directory is dirty and you want to continue work on a new branch, git stash branch is the fastest path — it creates a new branch from the stash point and applies its contents at once:

Move a stash to a new branch
git stash branch fitur/form-login
git status

This avoids the checkout conflicts that usually occur when stash contents do not match the target branch state. Once it succeeds, the stash is automatically removed from the list — no extra git stash drop needed.

Storing Untracked Files

Remember: git stash by default does not touch untracked files — new files that were never git add-ed remain in the working directory. If you also want to store those new files, add the -u flag (or -a to include ignored files):

Stash including untracked files
git stash -u
git status

With git stash -u, the working directory becomes truly clean — including new files. Without this flag, untracked files will "ride along" and potentially sneak into other branches.

Cleaning Untracked Files with Git Clean

Once the stash is taken, there may still be leftover untracked files — for example build artifacts or unwanted test files. This is where git clean comes in.

The golden rule: always check first with a dry-run before deleting anything.

Dry-run, then actually delete
git clean -n
git clean -fd
  • -n (dry-run) — lists the files and folders that would be deleted, without deleting.
  • -f (force) — actually deletes.
  • -d — also includes untracked directories.
  • -x — also includes ignored files (e.g. .env) — very dangerous.

Warning

git clean cannot be undone — deleted files are not in Git, so they cannot be recovered through the reflog. Always run git clean -n first, and never type git clean -xfd carelessly, because it can delete .env.local and other important files.

The combination most teams use: git clean -fd to discard ordinary untracked files and folders; git clean -n to audit before executing.

Closing

This episode completes your Git cleaning toolkit: git stash and git stash save "message" store temporary changes, git stash apply and git stash pop restore them, git stash list, git stash drop, and git stash clear manage the stack, git stash -u also stores untracked files, and git clean -n and git clean -fd clean up untracked files safely.

The points to take with you:

  • Stash stores changes without committing — perfect for sudden branch switches.
  • git stash pop takes and removes a stash; git stash apply only takes it.
  • git stash -u includes untracked files; by default it does not.
  • Always dry-run git clean -n before deleting with git clean -fd.
  • git clean cannot be undone — check twice before executing.

Stash tidies up the working directory; the next episode tidies up history itself. In episode 16 we cover Interactive Rebase & History Cleanup — combining commits, changing messages, and discarding commits before they are pushed. See you in episode 16!

Learn Git - Git Stash & Git Clean | Learn Git & GitHub