Lesson 38 of 6815 min read

git stash: Temporarily Saving Work in Progress

Learn how git stash lets you set aside uncommitted work temporarily, so you can switch contexts cleanly without committing incomplete changes.

Author: CodersNexus

git stash: Temporarily Saving Work in Progress

A very common situation: you're in the middle of some uncommitted work, but you suddenly need to switch branches — perhaps to fix an urgent bug (recall the hotfix branches from the previous lesson) — and your current changes aren't ready to be committed yet. `git stash` solves exactly this problem, letting you temporarily set aside your uncommitted work, leaving a clean working directory, so you can switch contexts freely and bring your work back later.

Learning Objectives

  • Save uncommitted changes temporarily using git stash.
  • Understand what git stash actually saves (both staged and unstaged changes).
  • Add a descriptive message to a stash for easier identification later.
  • Recognize typical scenarios where stashing is the right tool.

Key Terms to Know Before Learning git stash

  • git stash: A command that temporarily saves your uncommitted changes (both staged and unstaged) and reverts your working directory to match the last commit, leaving it clean.
  • Stash: The saved snapshot of uncommitted work created by git stash, stored in a stack-like structure for later retrieval.
  • git stash save "message" / git stash push -m "message": Variants of the stash command that let you attach a descriptive label to the saved stash.
  • Clean working directory: A state where there are no uncommitted changes, matching exactly what the last commit contains — the state git stash creates after running.

How git stash Actually Works

Running the basic stash command:

```
git stash
```

does two things simultaneously: it saves your current uncommitted changes (both staged content and unstaged working directory modifications) into a special, temporary storage area, and it reverts your working directory back to exactly match your last commit — giving you a clean state, as if you had never made those changes at all. Crucially, **this is not the same as discarding your changes** (Module 2, Lesson 5) — the changes are safely preserved in the stash, ready to be restored later.

This is exactly the right tool for the common scenario described in the introduction: you're partway through some work on a feature branch, an urgent request comes in to switch to `main` and create a hotfix branch, but your current changes aren't committed and aren't ready to be. Rather than making a messy, half-finished commit just to switch contexts, or risking Git's refusal to switch branches due to conflicting uncommitted changes (Lesson 3 of this module), you stash your work, switch freely, handle the urgent task, and return later to restore exactly where you left off.

By default, a bare `git stash` gives your saved work a generic, auto-generated description (like `WIP on feature/login: a1b2c3d Add validation`). For clarity — especially if you expect to have multiple stashes at once — it's good practice to add your own descriptive message:

```
git stash push -m "WIP: refactoring login validation, not ready to commit"
```

(`git stash save "message"` is an older, now slightly discouraged synonym for this same operation — `git stash push -m` is the currently recommended, more explicit syntax, though both still work.)

A quick but important clarification on scope: by default, `git stash` only stashes changes to files Git is already tracking (whether staged or unstaged) — it does **not** include completely untracked files by default. If you also want to stash new, untracked files, you need the `-u` (or `--include-untracked`) flag:

```
git stash -u
```

This distinction matters, since forgetting it can lead to confusion when a stash 'doesn't seem to include everything' — the next lesson covers how to inspect, restore, and manage multiple stashes, including checking exactly what each one contains.

git stash: Visual Walkthrough

Draw a three-panel sequence. Panel 1: 'Working on feature/login — uncommitted changes in app.js (staged) and style.css (unstaged)'. Arrow labeled 'git stash' points to Panel 2: 'Working directory now CLEAN (matches last commit) — changes safely saved in a stash'. Draw a side box labeled 'Stash storage' containing the saved snapshot. Panel 3, after switching branches and doing other work, shows an arrow labeled 'later: git stash pop (next lesson)' returning to a state with the original changes restored.

Command vs Effect: Key Differences

CommandEffect
git stashSaves staged + unstaged changes to tracked files; reverts working directory to last commit
git stash push -m "message"Same as above, with a custom descriptive message attached
git stash -uSame as git stash, but also includes untracked (new) files
git stash save "message" (older syntax)Equivalent to git stash push -m "message", still functional but less current

git stash: Command Syntax and Examples

# Basic stash: save current uncommitted work, clean the working directory
git stash
# Saved working directory and index state WIP on feature/login: a1b2c3d Add validation

# Stash with a custom, descriptive message
git stash push -m "WIP: refactoring login validation, not ready to commit"

# Stash including untracked (new) files as well
git stash -u

# Confirm the working directory is now clean
git status
# nothing to commit, working tree clean

Breaking Down the git stash Example

The basic `git stash` command demonstrates the default behavior, including Git's auto-generated description referencing the branch and last commit. `git stash push -m "..."` shows the recommended way to attach your own clearer, more specific message — especially valuable if you'll be stashing more than once before returning to this work. `git stash -u` demonstrates explicitly including untracked files, which a plain `git stash` would otherwise leave behind, still showing up as untracked in your 'cleaned' working directory. Finally, `git status` confirms the working directory is now completely clean, exactly as if the stashed changes had never been made — though, as the next lesson covers, they remain safely recoverable.

How git stash Is Used on Real Engineering Teams

  • Developers interrupted by an urgent bug report or an unplanned meeting frequently reach for git stash as an immediate, safe way to set aside in-progress work without a rushed, low-quality commit.
  • Switching to review a colleague's pull request often requires a clean working directory matching a different branch, making git stash a routine step before temporarily switching context to do a code review.
  • Some developers use git stash as a lightweight way to quickly test how their code behaves without a particular set of recent changes, stashing them temporarily, testing, and then deciding whether to restore or discard them.
  • Pair or mob programming sessions sometimes use stashing to let one person quickly hand off a clean working directory to another, without needing to make a messy intermediate commit.

git stash Interview Questions and Answers

Q1. What does git stash do, and how is it different from discarding changes?

git stash temporarily saves your current uncommitted changes (both staged and unstaged, for tracked files) into a special storage area, and reverts your working directory to match the last commit, leaving it clean. Unlike discarding changes, the stashed work is not lost — it's safely preserved and can be restored later.

Q2. Does git stash include newly created, untracked files by default?

No. By default, git stash only saves changes to files Git already tracks. To also include untracked (new) files, you need to add the -u (or --include-untracked) flag, as in git stash -u.

Q3. In what kind of real-world scenario would you use git stash?

A common scenario is being partway through uncommitted work on one branch when you urgently need to switch to another branch — for example, to address a critical bug. Rather than making a rushed, incomplete commit or risking Git refusing to switch due to conflicting changes, you stash your work, switch freely, handle the urgent task, and return later to restore exactly where you left off.

git stash Quiz: Test Your Understanding

1. What does a basic git stash command do to your working directory?

  1. Permanently deletes all uncommitted changes
  2. Saves uncommitted changes and reverts the working directory to match the last commit
  3. Commits all current changes automatically
  4. Creates a new branch containing the current changes

Answer: B. Saves uncommitted changes and reverts the working directory to match the last commit

Explanation: git stash safely saves your current uncommitted work into temporary storage and cleans your working directory back to the state of the last commit, without discarding anything.

2. Does git stash include untracked (new) files by default?

  1. Yes, always
  2. No — the -u flag is required to include untracked files
  3. Only if they are staged first
  4. Only files larger than 1MB

Answer: B. No — the -u flag is required to include untracked files

Explanation: By default, git stash only saves changes to files Git already tracks; untracked files are left out unless you explicitly add the -u (or --include-untracked) flag.

3. What is the recommended modern syntax for adding a custom message to a stash?

  1. git stash save "message"
  2. git stash push -m "message"
  3. git stash --message "message"
  4. git commit --stash "message"

Answer: B. git stash push -m "message"

Explanation: git stash push -m "message" is the currently recommended syntax for creating a stash with a custom description, though the older git stash save "message" still works as an equivalent.

git stash: Common Mistakes Beginners Make

  • Confusing git stash with discarding changes entirely — stashed work is safely preserved and recoverable, not deleted.
  • Forgetting the -u flag and being surprised that newly created, untracked files are still present after stashing.
  • Not adding a descriptive message when creating a stash, making it harder to identify later, especially if multiple stashes accumulate.
  • Stashing work and then forgetting about it entirely, letting stashes accumulate indefinitely rather than restoring or cleaning them up (a topic in the next lesson).

git stash: Exam-Ready Quick Notes

  • git stash: saves staged + unstaged changes (tracked files only), reverts working directory to last commit.
  • git stash -u: also includes untracked files.
  • git stash push -m "message": recommended syntax for adding a custom description (older synonym: git stash save "message").
  • Stashed work is safely preserved, not discarded — recoverable via commands covered in the next lesson.

git stash: Key Takeaways

  • git stash provides a safe way to temporarily set aside uncommitted work, giving you a clean working directory to switch contexts freely.
  • By default, only tracked file changes are stashed — untracked files require the explicit -u flag.
  • Adding a clear, descriptive message to each stash makes it much easier to manage if you accumulate more than one over time.

Frequently Asked Questions About git stash

Q1. What does git stash do?

It temporarily saves your current uncommitted changes and reverts your working directory back to match your last commit, giving you a clean state. Your changes aren't lost — they're safely stored and can be brought back later.

Q2. Is git stash the same as discarding my changes?

No. Discarding changes (using git restore, for example) permanently removes them. Stashing safely preserves your changes in temporary storage, allowing you to restore them later exactly as they were.

Q3. Does git stash save new files I haven't added to Git yet?

Not by default. A plain git stash only saves changes to files Git is already tracking. To include newly created, untracked files as well, use git stash -u (or the longer --include-untracked flag).

Q4. When would I actually need to use git stash?

A common situation is being in the middle of uncommitted work when you suddenly need to switch to a different branch — for example, to fix an urgent bug. Instead of making a rushed commit or losing your progress, you stash it, switch freely, and restore it later once you're ready to continue.

Q5. How do I add a description to my stash so I can identify it later?

Use git stash push -m "your description here", which attaches a custom, meaningful message to that specific stash, making it much easier to recognize later, especially if you end up with several stashes at once.

Summary

`git stash` temporarily saves your current uncommitted changes — both staged and unstaged, for files Git already tracks — into a special storage area, and reverts your working directory to exactly match the last commit, leaving it clean. This is distinct from discarding changes: stashed work is safely preserved and can be restored later, making it the ideal tool for situations like needing to urgently switch branches while in the middle of incomplete work. By default, `git stash` does not include newly created, untracked files, which requires the `-u` (`--include-untracked`) flag. Adding a custom message via `git stash push -m "message"` (the modern recommended syntax, replacing the older `git stash save "message"`) makes stashes easier to identify, especially once more than one accumulates — a topic covered in the next lesson on managing the stash stack.

Frequently Asked Questions

It temporarily saves your current uncommitted changes and reverts your working directory back to match your last commit, giving you a clean state. Your changes aren't lost — they're safely stored and can be brought back later.

No. Discarding changes (using git restore, for example) permanently removes them. Stashing safely preserves your changes in temporary storage, allowing you to restore them later exactly as they were.

Not by default. A plain git stash only saves changes to files Git is already tracking. To include newly created, untracked files as well, use git stash -u (or the longer --include-untracked flag).

A common situation is being in the middle of uncommitted work when you suddenly need to switch to a different branch — for example, to fix an urgent bug. Instead of making a rushed commit or losing your progress, you stash it, switch freely, and restore it later once you're ready to continue.

Use git stash push -m "your description here", which attaches a custom, meaningful message to that specific stash, making it much easier to recognize later, especially if you end up with several stashes at once.