CodeWithAbdessamad

Stashing

Advanced Git: Stashing

Stashing is one of Git’s most powerful yet underutilized features for professional workflows. It lets you temporarily save uncommitted changes without committing them, making it essential for context switching, feature work, and avoiding messy commit histories. Think of it as a “mental workspace” for your changes—perfect when you need to pause work on a branch to handle an urgent task or switch contexts. Let’s dive deep into how to master this technique.

Why Stashing Matters in Professional Workflows

Imagine you’re working on a critical feature branch with uncommitted changes, but your team needs you to fix a production bug in the main branch. Without stashing, you’d either:

  1. Commit your work (risking broken state if you later realize you need to revert)
  2. Discard changes (losing progress)
  3. Create a temporary commit (clutters history)

Stashing solves this by letting you safely pause your changes while working on other tasks. It’s especially valuable for:

  • Switching between branches without committing
  • Handling time-sensitive tasks (e.g., security fixes)
  • Avoiding “stale” commits that become confusing later

💡 Pro Tip: Stashing is not a replacement for git commit—it’s a temporary safety net for uncommitted work. Always commit when you’re ready to integrate changes.

The git stash Command: Core Operations

Stashing Changes

The simplest use case: save all uncommitted changes to a stash. This works when your working directory is dirty (has uncommitted changes).

<code class="language-bash">git stash</code>

Example Workflow:

  1. Create a new file feature.txt with uncommitted changes.
  2. Switch to another branch to fix a bug.
  3. Use git stash to save changes temporarily.
<code class="language-bash">echo "This is a feature" > feature.txt
<p>git add feature.txt</p>
<p>git stash</code>

What happens?

Your feature.txt is saved in the stash stack. The git stash command creates a new stash entry named stash@{0} (the most recent stash). Your working directory becomes clean.

Popping a Stash

Popping a stash applies it to your working directory and removes it from the stash stack. This is the reverse of stashing.

<code class="language-bash">git stash pop</code>

Example:

After fixing the bug in another branch, return to your feature branch with:

<code class="language-bash">git stash pop</code>

What happens?

  • Your feature.txt is restored to the working directory.
  • The stash is deleted from the stack (you can’t reuse it).
  • Warning: If your stash has conflicts, git stash pop will fail. Use git stash apply instead.

Listing Stashes

Check your stash stack with git stash list. This shows all stashes in order (most recent first).

<code class="language-bash">git stash list</code>

Example Output:

<code>stash@{0}: WIP on feature: 5e3f4a7 Add feature text
<p>stash@{1}: WIP on bugfix: 9a2b3c1 Fix critical bug</code>

Key Details:

  • stash@{0} = most recent stash (default)
  • stash@{1} = next most recent
  • The WIP on prefix shows the branch you were working on when you stashed

Applying a Stash

Apply a stash without removing it from the stack. This is useful when you want to reuse the same stash later.

<code class="language-bash">git stash apply <stash-id></code>

Example:

After popping a stash, you might want to apply it again later:

<code class="language-bash">git stash apply stash@{0}</code>

When to use apply vs pop:

Scenario Use pop Use apply
You want to reuse the stash
You want to remove the stash
Conflicts occur ❌ (fails) ✅ (resolves)

Managing Multiple Stashes

Stashing creates a stack—like a stack of plates. You can:

  • Save multiple stashes
  • Reapply stashes in order
  • Clean up old stashes

Example Workflow:

  1. Create 3 stashes in sequence:
<code class="language-bash">git stash save "First draft"</p>
<p>git stash save "Second draft"</p>
<p>git stash save "Third draft"</code>

  1. List stashes:
<code class="language-bash">git stash list</code>

Output:

<code>stash@{0}: First draft
<p>stash@{1}: Second draft</p>
<p>stash@{2}: Third draft</code>

  1. Apply the oldest stash first:
<code class="language-bash">git stash apply stash@{2}</code>

  1. Remove all stashes:
<code class="language-bash">git stash clear</code>

Stashing Specific Files

Stashing only specific files (not the whole working directory) is crucial for targeted changes.

<code class="language-bash">git stash save "File-specific changes" -u</code>

Example:

<code class="language-bash">echo "This is a test" > test.txt
<p>git add test.txt</p>
<p>git stash save "Test file change" -u</code>

Why -u?

The -u flag tells Git to stash only modified files (not staged changes). Without it, it would stash all uncommitted files.

Stashing Without Committing

This is the core purpose of stashing. You never commit your changes until you’re ready—stashing lets you:

  • Work on multiple tasks without committing
  • Avoid “half-commits” that become messy
  • Keep your commit history clean

Critical Insight:

Stashing does not commit your changes. It saves them temporarily. Your working directory remains clean after git stash.

Stashing in Real-World Scenarios

Scenario 1: Switching Branches Mid-Work

Problem: You’re on feature-xyz with uncommitted changes, but need to fix a bug in main.

Solution:

<code class="language-bash">git stash  # Save changes
<p>git checkout main</p>
<p>git merge feature-xyz  # Fix bug</p>
<p>git checkout feature-xyz</p>
<p>git stash pop  # Restore changes</code>

Scenario 2: Handling Conflicts

Problem: Stash has conflicts when applying.

Solution:

<code class="language-bash">git stash list  # Check conflict status
<p>git stash apply stash@{0}  # Apply without popping</p>
<h1>Resolve conflicts manually</h1>
<p>git stash pop  # Now pop after resolving</code>

Scenario 3: Stashing Multiple Changes

Problem: You have 3 files to stash but want to keep them separate.

Solution:

<code class="language-bash">git stash save "File A" -u
<p>git stash save "File B" -u</p>
<p>git stash list  # Verify separate stashes</p>
<p>git stash apply stash@{0}  # Apply File A first</code>

Common Pitfalls and Solutions

Pitfall Solution
Stash conflicts Use git stash apply instead of pop
Stashing too many files Use -u to stash only modified files
Forgetting to pop after applying git stash list to check stack status
Stashing committed changes Always run git stash on uncommitted work

Critical Reminder:

Never run git stash on a committed file. Git will fail with error: pathspec 'file' did not match any file(s) known to git. Always verify your working directory is clean before stashing.

Summary

Stashing is a professional-grade technique for managing uncommitted changes without disrupting your workflow. By understanding git stash save, git stash pop, git stash apply, and git stash list, you can:

  • Safely pause work when switching contexts
  • Avoid messy commit histories
  • Reuse changes across tasks
  • Handle conflicts without losing progress

Master stashing, and you’ll transform how you work with Git—turning temporary interruptions into opportunities for cleaner, more reliable workflows. 🚀