Git Stash: Managing Work in Progress Without Committing

When Stash Is Useful

You are mid-feature when a production bug comes in. You cannot commit half-finished work. git stash saves your changes to a temporary stack so you can switch branches cleanly.

---

Basic Stash Workflow

# Save current working directory changes
git stash push -m "WIP: auth refactor"

# Your working directory is now clean
git checkout main
git checkout -b hotfix/login-redirect
# ...make and commit the fix...
git checkout feature/auth-refactor

# Restore your stashed work
git stash pop          # apply most recent stash and remove it from stack

---

Stash Commands

git stash list                      # see all stashes
git stash show                      # summary of most recent stash
git stash show -p                   # full diff of most recent stash
git stash show stash@{2}            # show a specific stash

git stash pop                       # apply + remove most recent stash
git stash apply                     # apply but keep in the stash list
git stash apply stash@{2}           # apply a specific stash

git stash drop                      # remove most recent stash
git stash drop stash@{2}            # remove a specific stash
git stash clear                     # remove ALL stashes (careful)

---

Stashing Untracked Files

By default, git stash only saves tracked files. New files you have not staged are ignored.

git stash push -u        # include untracked files
git stash push -a        # include untracked AND gitignored files (careful)

---

Creating a Branch from a Stash

If you stashed work and now realize it should be on its own branch:

git stash branch feature/new-feature stash@{0}

This creates a new branch at the commit where the stash was made, applies the stash, and drops it from the list if there are no conflicts.

---

Partial Stash

git stash push -p    # interactive — choose which hunks to stash

Useful when you have two sets of changes mixed together and want to stash only one.

---

Recovering a Dropped Stash

git stash drop and git stash clear remove stash entries. The underlying objects are still in Git's object store for a while (until git gc runs).

# Find dangling commits (stash entries are stored as commits)
git fsck --unreachable | grep commit

# Show the content of a specific dangling commit
git show <hash>

# If it is your stash, apply it
git stash apply <hash>

This works if git gc has not run yet (typically 2 weeks on default settings).

---

Stash vs WIP Commits

An alternative to stash: commit with a WIP message, then amend or reset later.

git add -A && git commit -m "WIP: auth refactor"
# switch branches, do hotfix, come back
git reset HEAD~1    # undo the WIP commit, keep changes in working directory

WIP commits are safer than stash for long-running context switches — they cannot be accidentally dropped and are easier to inspect.

Practice in the ShellGenius Git Labs — the git-staging and git-undo-changes challenges cover real staging and recovery workflows.