Skip to main content

Stashing Your Work

Imagine you are deep into coding a complex "Dark Mode" feature for CodeHarborHub. Your workspace is messy, and your code isn't finished. Suddenly, a critical bug is found on the live site that you need to fix immediately.

You can't commit your "Dark Mode" code because it's half-finished, and Git won't let you switch branches while you have unsaved changes.

The solution? Use git stash.

What is Stashing?

git stash takes your uncommitted changes (both staged and unstaged) and "hides" them in a temporary storage area within Git. This gives you a clean working directory so you can switch tasks instantly.

Analogy: It’s like being in the middle of a puzzle on your dining table. If you need the table for dinner, you don't throw the puzzle away; you slide it onto a tray and put it in a drawer. When dinner is over, you slide the tray back onto the table and continue exactly where you left off.

The Stashing Workflow

1. Hide your work

When the interruption happens, run:

git stash

Your code is now safe, and your folder looks exactly like it did at your last commit.

2. Switch and Fix

Now you can safely switch branches to fix that urgent bug:

git checkout main
# ... fix bug, commit, and push ...

3. Bring your work back

Switch back to your feature branch and retrieve your hidden work:

git checkout feat-dark-mode
git stash pop

The pop command takes the work out of the "drawer" and puts it back into your files.

Managing the "Stash Stack"

You can stash multiple times! Git keeps them in a "Stack" (the newest stash is always on top).

CommandAction
git stash listSee all your "hidden trays" in the drawer.
git stash applyBring work back but keep a copy in the stash (safer than pop).
git stash dropDelete the most recent stash without applying it.
git stash clearEmpty the entire drawer (Delete all stashes).

Advanced Stashing

If you have many stashes, give them names so you don't get confused:

git stash save "UI work in progress"

When to use Stashing at CodeHarborHub?

  1. Context Switching: When a teammate needs you to review their code immediately.
  2. Pulling into a Dirty Branch: If you try to git pull but have local changes that conflict, stash your work, pull, and then pop your stash back.
  3. Experimenting: If you want to try a crazy idea but aren't sure it will work, stash your current stable work first.
tip

Before you git stash pop, run git status. It’s always best to be on the same branch you were on when you created the stash to avoid unnecessary merge conflicts!