Applying and Clearing Git Stashes

In the previous post, we discussed how to use git stash to temporarily save changes without committing them. Now, we'll dive deeper into how to apply and clear stashes when you're ready to bring your changes back or clean up your stash history.

Applying and Clearing Git Stashes

Overview

In the previous post, we discussed how to use git stash to temporarily save changes without committing them. You can check it here. Now, we'll dive deeper into how to apply and clear stashes when you're ready to bring your changes back or clean up your stash history.

1. What Happens When You Apply a Stash?

When you stash changes with git stash, the changes are saved and removed from your working directory, allowing you to switch branches or pull updates with a clean workspace. Later, when you're ready to retrieve those changes, you can apply or pop the stash.

Applying a stash does the following:

  • It reapplies the stashed changes to your working directory.
  • Your working directory will contain both the stashed changes and any other modifications you've made.
  • The stash remains in the stash list unless you use the pop command (which removes the stash after applying it).

2. How to Apply a Specific Stash

By default, when you apply a stash, Git reapplies the most recent stash (i.e., stash@{0}). However, if you have multiple stashes, you may want to apply an older stash or a specific stash from the list.

Applying the Latest Stash

To apply the most recent stash (the one created last), use:

git stash apply

This will restore the changes from the latest stash (stash@{0}) to your working directory, but it won't remove the stash from the list.

Applying a Specific Stash

If you want to apply an older stash or a specific stash, you can refer to its identifier (e.g., stash@{1}, stash@{2}, etc.).

First, use the following command to list all the available stashes:

git stash list

The output will look something like this:

stash@{0}: WIP on feature-branch: 34ab7c9 Initial feature commit
stash@{1}: WIP on main: 56bde7f Fix typo in README
stash@{2}: WIP on bugfix-branch: 12ef9ab Debugging login issue

Once you know the identifier of the stash you'd like to apply, you can use it like this:

git stash apply stash@{1}

This will apply the changes saved in the stash stash@{1} without removing it from the stash list.

3. Applying and Removing (Popping) a Stash

In some cases, after applying a stash, you no longer need it and want to remove it from the stash list automatically. Instead of applying the stash and manually removing it afterward, you can use the git stash pop command, which combines both actions.

Popping the Latest Stash

To apply the most recent stash and remove it from the stash list in one go, use:

git stash pop

This command will:

  • Apply the latest stash (stash@{0}) to your working directory.
  • Remove the stash from the stash stack.

Popping a Specific Stash

You can also pop a specific stash if you have multiple stashes. For example, to pop stash@{1}, you would run:

git stash pop stash@{1}

This will apply the changes saved in stash@{1} to your working directory and remove stash@{1} from the stash list.

Tip: If you want to check which stash has been popped (removed), you can list all remaining stashes after running the pop command with git stash list.

4. Managing Multiple Stashes

As you work, you may accumulate multiple stashes in your stash stack. Git stores these stashes in a stack-like structure, where the most recent stash is at the top (i.e., stash@{0}). You can manage and apply any stash from the list at any time.

Viewing the List of Stashes

To see all the stashes currently saved in your repository, use:

git stash list

Each stash is displayed with an identifier (e.g., stash@{0}, stash@{1}), along with a description that includes the branch name and the first few characters of the commit hash.

Example output:

stash@{0}: WIP on feature-branch: 34ab7c9 Initial feature commit
stash@{1}: WIP on main: 56bde7f Fix typo in README
stash@{2}: WIP on bugfix-branch: 12ef9ab Debugging login issue

5. How to Clear Individual Stashes

Once you're done with a specific stash and no longer need it, you can remove it from the stash list. Git provides the git stash drop command for this purpose.

Dropping a Specific Stash

To delete (or drop) a specific stash from the list, use:

git stash drop stash@{1}

This command will remove stash@{1} from the stash list, but it won’t affect your working directory.

Dropping the Most Recent Stash

If you want to drop the most recent stash (stash@{0}), you can run the following command:

git stash drop

This is shorthand for git stash drop stash@{0}, which removes the latest stash from the stash stack.

6. How to Clear All Stashes at Once

If you're done with all your stashes and want to clear them in one go, Git provides a command to remove every stash in your stash stack.

Clearing All Stashes

To delete all stashes from the repository, run:

git stash clear

This will remove every stash saved in the repository, leaving you with an empty stash list. Be careful when using this command, as clearing stashes cannot be undone!

7. Best Practices for Managing Stashes

Here are some tips and best practices to effectively manage your stashes:

1. Name Your Stashes for Better Tracking

When you create a stash, it's a good idea to add a custom message to describe what changes are being saved. This helps you identify specific stashes later when you have multiple stashes saved.

git stash push -m "WIP: Fixing login issue"

2. Regularly Clear Unused Stashes

Stashes can accumulate over time, so make it a habit to clean up old or unused stashes to keep your stash list manageable.

git stash clear  # To clear all stashes

Alternatively, you can remove individual stashes that you no longer need:

git stash drop stash@{0}

3. Check for Conflicts After Applying Stashes

When applying stashes, it's important to check if there are any conflicts. Since stashes restore uncommitted changes, they can sometimes result in merge conflicts if the underlying files have been modified since the stash was created.

After applying a stash, carefully review your files for any conflicts. If conflicts arise, Git will mark them in the same way it handles merge conflicts, and you can resolve them using git status and editing the conflicted files.

4. Avoid Overusing Stashes

While stashing is a useful tool, it should not be overused in place of proper branching and committing workflows. Stashing is meant to be a temporary solution for saving work in progress, not a long-term method for managing changes.

Consider committing small, incremental changes to a feature branch instead of relying on stashes for long-term storage. This keeps your work better organized and helps prevent lost progress.

Conclusion

The git stash command is a powerful feature for temporarily saving uncommitted changes in Git. However, it's equally important to know how to apply and clear stashes effectively to maintain a clean and organized workspace.

In this post, we discussed:

  • How to apply the latest or specific stashes.
  • The difference between applying and popping stashes.
  • How to drop individual stashes or clear all stashes at once.
  • Best practices for managing stashes.

By mastering the stash functionality, you'll be able to efficiently manage uncommitted changes and switch between tasks without losing progress.

Stay tuned for the next post, where we'll dive into more advanced Git topics and workflows!

Read next

Step-by-Step with Git: Advanced Commit Strategies

Let’s use a practical example to guide us through this process. By the end of this tutorial, you'll understand how to commit changes selectively, review modifications, and manage staged and unstaged changes.

Using Git Stash in Multi-Branch Workflows

You're likely to work with multiple branches for different features, bug fixes, and releases. While managing these branches, you might run into situations where you need to temporarily save your work without committing it, and later apply it on the same or even a different branch.

Git Stash: Temporarily Saving Changes

When you need to quickly switch branches, but you have uncommitted changes in your working directory. Committing these unfinished changes might not be desirable, and discarding them would result in a loss of your work. This is where Git’s stash command comes into play.