git stash temporarily shelves changes in your working directory and index, restoring your repository to a clean state (matching HEAD). It is a stack-based mechanism — you can push multiple stashes and pop them in LIFO order.
Switching branches with uncommitted work you cannot commit yet. Pulling remote changes that conflict with your local work. Running a quick test on a clean working directory. Context-switching between tasks. but they are not visible in `git log` and can be accidentally dropped. If your changes are substantial, commit them on a branch instead.A stash is stored as a special commit with a tree of your working directory + index changes . Each stash entry is actually a merge commit with two (or three) parents:
gitGraph
commit id: "W (working tree changes)"
commit id: "I (index changes)"
checkout W
merge I id: "S (stash commit)" Parent 1 : The commit HEAD was at when the stash was created.Parent 2 : The index state (if --keep-index was used).Working tree changes : Stored as the stash commit”s tree.$ git stash show -p stash@{ 0 }
## Shows the diff that the stash represents
# Show the stash as a commit
$ git log --oneline --graph stash@{ 0 }
# Stash all tracked changes (working directory + index)
# Stash with a message (recommended for multiple stashes)
$ git stash push -m " WIP: auth middleware "
# Stash only tracked files (ignore untracked files)
# Stash including untracked files
# Or: git stash --include-untracked
# Stash everything (tracked + untracked + ignored)
# Stash working directory changes but keep staged changes in the index
$ git stash push --keep-index
This is useful when you want to test only your staged changes:
$ git stash push --keep-index -m " Test staged changes "
$ npm test # Test only the staged changes
$ git stash pop # Restore everything
stash@ {0} : On feature-auth: WIP: login page
stash@ {1} : On main: experimental refactor
stash@ {2} : On feature-auth: WIP: auth middleware
# Apply the most recent stash (keep it in the stack)
$ git stash apply stash@{ 1 }
# Apply and remove from the stack (LIFO)
# Apply stash to a different branch
$ git switch feature-auth
$ git stash apply stash@{ 0 }
# Drop the most recent stash
$ git stash drop stash@{ 1 }
You can stash specific files or hunks:
$ git stash push src/auth.c src/login.c -m " Auth changes "
# Interactive stash (choose hunks)
If a stash was created on a branch that has since diverged, applying it may cause conflicts. Creating a branch from the stash applies it in a clean context:
$ git stash branch stash-branch-name stash@{ 0 }
# Creates a new branch from the stash's base commit
# Drops the stash from the stack
The stash command does not run pre-commit hooks. This is by design — stashing is a temporary operation, not a commit. However, this means:
Linters and formatters configured as pre-commit hooks will not run on stashed changes. Tests configured as pre-commit hooks will not validate stashed changes. If you need hook validation before stashing, commit to a temporary branch instead:
# ... continue working ...
Limitation Workaround Cannot stash merge conflicts Resolve conflicts or abort merge first Stashes are not visible in git log Use git stash list and git stash show -p Stashes can be accidentally dropped Use descriptive messages; reflog retains dropped stashes Binary files in stashes take up space Use git stash --keep-index to avoid re-staging binaries No automatic expiry Periodically git stash clear or git stash drop old entries
Neglecting to normalise database designs, leading to data redundancy and update anomalies.
Misunderstanding the difference between a stack (LIFO) and a queue (FIFO) in data structure applications.
Forgetting edge cases in algorithm design (e.g., empty input, single element, already sorted data).
Confusing an algorithm with a program. An algorithm is a step-by-step procedure, not its implementation in code.
The key principles covered in this topic are linked in the sub-pages above. Focus on understanding the definitions, applying the formulas or frameworks, and evaluating strengths and limitations of each approach.
Worked examples demonstrating the application of key concepts are covered in the detailed sub-pages linked above.
Stash is like a “pause button” for your work. Imagine you are writing a letter and someone asks you to check something else quickly. Instead of finishing the letter or throwing it away, you put it in a drawer (stash) and come back to it later. The drawer is a stack - the last letter you put in is the first one you take out. Stash is useful when you need to switch tasks but your current work is not ready to commit. The key insight is that stash is a convenience mechanism, not a replacement for commits - it stores temporary changes that you intend to reapply soon. For longer-term storage, create a branch and commit.
Reflog - How to recover stashes that were accidentally droppedBisect - Another advanced Git technique for finding bugs