# The Git Stash

> Temporarily save messy, uncommitted work so you can safely switch context.

Subject: Version Control | Lesson 27 of 29 | 6 min read
Interactive lesson: https://codewithanimation.in/git/git-stash
Previous: [Forking & Cloning](https://codewithanimation.in/git/forking-and-cloning.md) | Next: [Bisecting Bugs](https://codewithanimation.in/git/git-bisect.md)

## The Temporary Shelf

`git stash` takes the dirty state of your working directory (modified tracked files and staged changes) and saves it onto a stack of unfinished changes.

It then cleans your working directory so it exactly matches the HEAD commit, giving you a clean slate.

## Why it matters

Web developers constantly juggle tasks. You might be halfway through building a complex React component when an urgent bug report requires you to switch branches immediately.

You cannot commit broken code, and you cannot switch branches with conflicting uncommitted changes. The stash allows you to save this messy work temporarily, fix the bug, and seamlessly restore your progress later.

## How it works

**Pushing to the Stack:** Running `git stash` packages your current changes into a temporary commit and pushes it onto a local stack (a Last-In, First-Out queue).

**Popping/Applying:** When you return, `git stash pop` applies the most recent stashed changes to your working directory and removes them from the stack. `git stash apply` does the same but leaves a copy on the stack.

## Common mistake: The Stash Hoarder Problem

A huge misconception is treating the stash as a permanent storage bin.

Developers often run `git stash` repeatedly without popping older stashes, creating a massive, untracked pile of forgotten code snippets. Because basic stashes lack descriptive names, attempting to apply a months-old stash often results in massive merge conflicts and lost context.

## Tip: Always Name Your Stashes

Mental Model: *Think of the stash as a clipboard for your branch.*

Pro Tip: Instead of just typing `git stash`, always use `git stash push -m "WIP: Header styling"`. This makes it trivial to figure out what is what when you run `git stash list`.

## Try it: Put a working change aside

Stash a sample change, switch branches and restore the saved work.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-stash

## Check yourself

### Question 1

What happens to your working directory immediately after you run `git stash`?

- A. It creates a new branch with your uncommitted changes.
- B. It commits the files to the current branch but marks them as 'stashed'.
- C. It reverts to the state of the last commit (HEAD), giving you a clean working tree.
- D. It pushes your uncommitted changes to the remote repository.

Correct answer: C

`git stash` saves your modifications and then cleans the working directory to match the HEAD commit, allowing you to safely switch branches.

- Why not A: Stashing doesn't create branches, it stores changes in a stack.
- Why not B: Stashes are not part of your branch's commit history.
- Why not D: Stashes are entirely local.

### Question 2

What is the key difference between `git stash pop` and `git stash apply`?

- A. pop removes the stash from the stack after applying it, while apply leaves it on the stack.
- B. pop only restores staged files, while apply restores untracked files.
- C. pop creates a new branch, while apply merges changes into the current branch.
- D. apply can resolve merge conflicts automatically, but pop cannot.

Correct answer: A

`git stash pop` essentially runs `git stash apply` followed by `git stash drop`. Use `apply` if you might want to apply those same stashed changes to multiple different branches.

- Why not B: Both restore the same set of changes.
- Why not C: Neither command creates a branch.
- Why not D: Both can encounter merge conflicts that require manual resolution.

### Question 3

Which command is the modern, recommended way to save your changes with a descriptive message?

- A. git stash push -m "WIP: Navbar"
- B. git stash save --name "WIP: Navbar"
- C. git stash -m "WIP: Navbar"
- D. git commit --stash "WIP: Navbar"

Correct answer: A

`git stash push -m "<message>"` allows you to add a label, making it much easier to identify the stash later when using `git stash list`.

- Why not B: `git stash save` is deprecated in favor of `push`.
- Why not C: This syntax is invalid.
- Why not D: There is no `--stash` flag for the commit command.

## Remember this

- Stash saves uncommitted work and cleans your working directory.
- It allows you to safely switch branches mid-task.
- Use `stash pop` to apply and delete, or `stash apply` to just apply.
- Always name your stashes using `stash push -m`.
