Module 6 Lesson 1: Git stash: saving and restoring work in progress
·DevOps

Module 6 Lesson 1: Git stash: saving and restoring work in progress

Pause your work without committing. Learn how to use 'git stash' to temporarily set aside your changes so you can switch branches and fix urgent bugs without losing your progress.

Module 6 Lesson 1: Git stash: saving and restoring work in progress

Imagine you are in the middle of a complex UI refactor. Your code is broken and half-finished. Suddenly, an urgent bug report comes in: the login button on main is broken.

You need to switch to main now, but Git won't let you switch branches because you have uncommitted changes that would be overwritten. You don't want to make a "half-finished" commit just to switch. What do you do?

You Stash it.


1. What is git stash?

git stash takes all your uncommitted changes (both staged and unstaged) and saves them in a temporary "stack." It then returns your working directory to the clean state of your last commit.


2. Fundamental Stash Commands

Saving a Stash

git stash

*(Optionally, add a message so you remember what it was: git stash save "Working on header refactor")

Seeing Your Stashes

git stash list

Each stash is identified by an index like stash@{0}, stash@{1}, etc.

Restoring a Stash

There are two ways to bring your work back:

  1. git stash apply: Applies the changes back to your files but keeps the stash in the list (good if you want to apply the same change to multiple branches).
  2. git stash pop: Applies the changes and deletes the stash from the list (the most common method).
graph LR
    Working["Working Directory (Dirty)"] -- "git stash" --> Stack["Stash Stack"]
    Stack -- "git stash pop" --> Working
    Working -- "Clean for Switch" --> Switch["Another Branch"]

3. Advanced Stashing

Stashing Untracked Files

By default, git stash only saves files that Git already knows about. To include new files you just created:

git stash -u

Stalling a Specific Stash

If you have multiple stashes, you can apply a specific one:

git stash apply stash@{2}

Clearing the Stack

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

# Delete ALL stashes
git stash clear

Lesson Exercise

Goal: The "Panic Switch" simulation.

  1. Go to your main branch.
  2. Edit a file to add some "half-finished" code. Do not commit it.
  3. Try to switch to another branch: git switch feature-x. Notice Git stops you.
  4. Run git stash.
  5. Notice your local changes are gone and your folder is clean.
  6. Now, successfully switch to feature-x.
  7. Switch back to main.
  8. Run git stash pop.
  9. Notice your "half-finished" code is back exactly where you left it!

Observation: You'll realize that stash is like a "Save State" in a video game. It allows you to pause reality and come back to it later.


Summary

In this lesson, we established:

  • git stash saves uncommitted work to a temporary stack.
  • It allows you to switch branches without making "messy" commits.
  • pop restores and deletes, while apply only restores.
  • -u is necessary if you have new, untracked files in your stash.

Next Lesson: We’ll explore one of the most controversial but powerful tools in Git: Lesson 2: Git rebase: linearizing history.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn