Class 20 of 50 • Branching & Merging

Git Stash: Temporarily Storing Working Changes, Stack Inspection, Pop & Apply

You are halfway through implementing a complex feature when an urgent production bug alert arrives. You must switch to main immediately to fix it. But you don't want to create an embarrassing "half-done unfinished code" commit. Git Stash takes your dirty working state, saves it to an internal clipboard stack, and restores a pristine working directory.

Git Stash Lifecycle Diagram
Figure 20.1: Git Stash Lifecycle — Stashing Dirty Changes to LIFO Stack and Restoring via Pop or Apply.

Basic Stashing: Saving Your Work

# Quick stash with default message:
$ git stash

# Recommended: Stash with a descriptive note:
$ git stash push -m "WIP: unfinished refactor of auth middleware"
Saved working directory and index state On feature/auth: WIP: unfinished refactor of auth middleware

Your working directory is now completely clean (matching HEAD), allowing you to switch branches safely!

Stashing Untracked Files with -u

By default, git stash only saves tracked files. If you created brand-new files that haven't been committed before, pass -u (or --include-untracked):

$ git stash -u -m "WIP: include new component templates"

Inspecting Your Stash Stack

Git maintains your stashes in a Last-In, First-Out (LIFO) stack:

# List all saved stashes:
$ git stash list
stash@{0}: On feature/auth: WIP: unfinished refactor of auth middleware
stash@{1}: On main: WIP: temporary database connection experiment

# Inspect the diff of the top stash without applying it:
$ git stash show -p stash@{0}

Restoring Stashed Changes: Pop vs Apply

1. git stash pop (Apply AND Remove)

Re-applies the changes from stash@{0} to your current working directory and immediately deletes it from the stash stack:

$ git stash pop

2. git stash apply (Apply and KEEP on Stack)

Applies the changes to your working directory but leaves the stash saved in your stash stack (ideal if you want to apply the same changes to multiple branches):

$ git stash apply

Targeting a Specific Stash Index

# Apply a specific stash from history:
$ git stash apply stash@{1}

Cleaning Up Your Stash Stack

# Delete a specific stash:
$ git stash drop stash@{0}

# Delete ALL stashes across your entire repository:
$ git stash clear

Turning a Stash into a New Branch (git stash branch)

If you stashed code, switched branches, and upon trying to pop your stash you encounter massive merge conflicts, turn the stash into its own dedicated branch:

$ git stash branch feature/resumed-auth stash@{0}

Git checks out the exact commit you were on when you created the stash, creates a new branch named feature/resumed-auth, and cleanly pops your stash with zero conflicts!

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Discarding vs Stashing Changes

Choose between permanently discarding edits or saving them to the stash stack.

Switching Branches Cleanly

Prevent checkout errors caused by dirty uncommitted working tree modifications.

Creating Stash Branches

Turn complex stashes into dedicated feature branches with git stash branch.

Hands-on Practice Lab Scenarios

Practice stashing untracked files, applying specific stash indices, and popping.

Frequently Asked Questions (FAQ) - Student & Interview Review

`git stash pop` applies the stash AND deletes it from the stash stack. `git stash apply` applies the modifications to your working tree but retains the stash in the stack for future use.
Yes! Pass `--keep-index`: `git stash --keep-index`. This stashes your unstaged changes while leaving your currently staged changes in the Staging Area Index.
Stashes are stored in the `.git/refs/stash` reference file, and the stash stack log is kept inside `.git/logs/refs/stash`.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to Managing Remotes → Previous: ← Interactive Rebase (-i) | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!