Skip to main content

Git Stash and Temporary Changes

Sometimes you're in the middle of working on a feature when an urgent bug fix or another task requires your immediate attention. Your current changes aren't ready to be committed, but you need a clean working directory. This is where Git stash comes to the rescue!

In this lesson, you'll learn how to:

  • Temporarily save uncommitted changes using git stash
  • Apply stashed changes back to your working directory
  • Manage multiple stashes and view stash history
  • Clean up stashes you no longer need
  • Use stash in real-world development scenarios

What is Git Stash?

Git stash allows you to temporarily save your uncommitted changes (both staged and unstaged) and revert your working directory to match the HEAD commit. Think of it as a "shelf" where you can put your work aside temporarily.

tip

Stash is perfect for when you need to:

  • Switch branches with uncommitted changes
  • Pull latest changes from remote
  • Work on an urgent fix while in the middle of feature development
  • Test something quickly without losing your current progress

Basic Stash Operations

Stashing Your Changes

When you have uncommitted changes that you want to set aside:

Stash current changes
git stash

This command saves your modifications and reverts the working directory to match the HEAD commit.

Let's see a complete example:

Complete stash workflow
# Create a new file and make some changes
echo "console.log('Feature in progress');" > feature.js
git add feature.js
echo "// More feature work" >> feature.js

# Check the status - we have both staged and unstaged changes
git status

# Stash everything
git stash

# Verify working directory is clean
git status

# Now you can safely switch branches or pull changes
git checkout main
git pull origin main

# Switch back to your feature branch
git checkout feature-branch

# Restore your stashed changes
git stash pop

Stashing with a Description

For better organization, especially when working with multiple stashes, add a descriptive message:

Stash with description
git stash push -m "WIP: user authentication feature"

Managing Multiple Stashes

Viewing Your Stashes

To see all your saved stashes:

List all stashes
git stash list

This shows output like:

stash@{0}: On feature-branch: WIP: user authentication feature
stash@{1}: On main: quick fix attempt
stash@{2}: On develop: experimental changes

Applying Specific Stashes

You can apply a specific stash without removing it from the list:

Apply specific stash
git stash apply stash@{1}

Or apply the most recent stash:

Apply latest stash
git stash apply

Removing Stashes

When you're done with a stash, you can remove it:

Remove specific stash
git stash drop stash@{1}

Or clear all stashes:

Clear all stashes
git stash clear

Advanced Stash Techniques

Stashing Specific Files

You don't always need to stash everything. Stash specific files instead:

Stash specific files
git stash push -- feature.js config.json

Stashing Untracked Files

By default, git stash doesn't save untracked files. Include them with:

Stash including untracked files
git stash -u

Or for untracked files only:

Stash only untracked files
git stash --include-untracked

Creating a Branch from a Stash

If a stash has been sitting around for a while, you might want to create a proper branch from it:

Create branch from stash
git stash branch new-feature-branch stash@{0}

This creates a new branch and applies the stash to it, then drops the stash.

Real-World Scenario: Handling Urgent Fixes

Let's walk through a common scenario where stash proves invaluable:

Handling urgent bug fix
# You're working on a new feature
git checkout feature/user-profile
echo "// Building user profile component" > user-profile.js
git add user-profile.js
echo "// More profile work" >> user-profile.js

# Urgent bug reported! Need to switch to main
git stash push -m "user profile WIP"

# Switch to main and create fix branch
git checkout main
git pull origin main
git checkout -b hotfix/login-issue

# Fix the bug
echo "// Fix login redirect" > login-fix.js
git add login-fix.js
git commit -m "Fix login redirect issue"

# Merge the fix
git checkout main
git merge hotfix/login-issue
git push origin main

# Back to your feature
git checkout feature/user-profile
git stash pop

# Continue working right where you left off!
warning

Be careful with git stash pop! Unlike git stash apply, pop removes the stash from the list after applying it. If there are conflicts during application, you might lose the stash. Consider using apply first to test, then drop if successful.

Common Pitfalls

  • Forgetting stash descriptions: Without messages, it's hard to remember what each stash contains, especially after some time has passed.

  • Stash conflicts: When applying a stash creates conflicts, resolve them carefully. The stash still exists until you drop it or use pop.

  • Long-lived stashes: Stashes aren't meant for long-term storage. If you find yourself keeping stashes for days, consider creating a proper branch instead.

  • Lost stashes: Stashes are local to your repository and aren't pushed to remote. Don't rely on them for backup.

  • Overwriting work: Applying a stash on top of uncommitted changes can lead to confusion. Always ensure a clean working state before applying stashes.

Summary

Git stash is an essential tool for managing temporary changes in your workflow. You learned how to:

  • Save uncommitted changes with git stash
  • Apply and remove stashes with apply, pop, and drop
  • Manage multiple stashes using the stash list
  • Use advanced techniques like stashing specific files and creating branches from stashes
  • Apply stash in real-world scenarios like handling urgent fixes

Remember that stashes are temporary and local-only. Use them for short-term context switching, but commit your work or create branches for longer-term storage.

Using Git Stash – Quick Check

What command temporarily saves your uncommitted changes and cleans your working directory?

Question 1/5