How to Apply Stashed Changes in Git

When working on a Git project, there are times when you need to quickly switch contexts — perhaps to fix a critical bug or pull the latest changes — but you’re not quite ready to commit your current work. This is where git stash comes in handy.

Git’s stash feature temporarily shelves (or “stashes”) changes you’ve made to your working directory, so you can work on something else, then come back and re-apply them later.

In this post, we’ll focus on how to apply stashed changes using Git, along with best practices and tips for managing your stashes.


What Is Git Stash?

git stash saves your uncommitted changes (both staged and unstaged) and reverts your working directory to match the HEAD commit. This lets you switch branches or pull updates without affecting your current work.

You can later apply or pop the stashed changes when you’re ready to resume work.


Stashing Recap

To stash changes:

git stash

To see a list of all your stashes:

git stash list

You’ll see entries like:

stash@{0}: WIP on main: 1234567 Updated feature X
stash@{1}: WIP on develop: 89abcde Debugging user login

Applying Stashed Changes

Option 1: git stash apply

This command reapplies the most recent stash without removing it from the stash list.

git stash apply

To apply a specific stash (e.g., the second one):

git stash apply stash@{1}

⚠️ If you apply a stash that conflicts with your current code, Git will mark the conflicts for manual resolution.


Option 2: git stash pop

This command applies the most recent stash and removes it from the stash list.

git stash pop

Or apply and pop a specific stash:

git stash pop stash@{1}

This is useful when you’re confident you don’t need to reuse the stashed changes again.


Additional Tips

Check What’s in a Stash

Before applying, you can inspect the changes in a stash:

git stash show stash@{0}

To see a full diff:

git stash show -p stash@{0}

Apply Stash to a Different Branch

You can switch to another branch before applying the stash:

git checkout feature-branch
git stash apply stash@{0}

This is handy when you realize the changes belong on another branch.


Clean Up Stashes

To delete a specific stash:

git stash drop stash@{0}

To clear all stashes:

git stash clear

⚠️ Be cautious — git stash clear deletes all stashed changes and cannot be undone.


Wrapping Up

Git stash is a powerful tool that helps you manage interruptions and work context shifts smoothly. Knowing how to apply stashed changes effectively can save you from losing work and improve your development workflow.

Here’s a quick summary:

TaskCommand
Apply latest stashgit stash apply
Apply and remove stashgit stash pop
Apply specific stashgit stash apply stash@{n}
View stash listgit stash list
Inspect stash contentsgit stash show -p stash@{n}
Delete a stashgit stash drop stash@{n}
Clear all stashesgit stash clear
Sharing Is Caring:

Leave a Comment