Stashing Changes in Git

Understanding how to effectively use git stash can significantly streamline your workflow, especially when handling multiple tasks within the same repository.

May 3, 20245 min read

Git offers a powerful feature known as "stashing" that allows developers to temporarily save changes without committing them. This tutorial will guide you through the essentials of using git stash, managing multiple stashes, and applying stashed changes back to your working directory.

1. How to Stash Changes with Git Stash

Stashing is particularly useful when you need to switch contexts quickly—say, from working on a new feature to fixing a critical bug—but you're not ready to commit the changes in your current branch. Here's how you can stash your changes:

Step 1: Adding Changes to Stash

To stash your changes, use the following command in your terminal:

git stash

This command temporarily stores all modified tracked files and stages changes, and then reverts them from your working directory. If you have any untracked or ignored files that you want to stash, you can use:

git stash -u  # Stashes untracked files as well
git stash -a  # Stashes all files, including ignored ones

Step 2: Naming Your Stash

For better manageability, you can name your stash for future reference:

git stash save "feature-x-progress"

This is particularly useful when working with multiple stashes.

2. Managing Multiple Stashes

You might often find yourself juggling multiple tasks in the same repository. Git allows you to create and manage multiple stashes.

Viewing Your Stashes

To see a list of all your stashed changes, use:

git stash list

This will display a list of stashes, each identified by a stash index, starting from stash@{0} for the most recent stash.

Creating Multiple Stashes

Simply run the git stash command multiple times to create more stashes. Each new stash is placed on top of the stack.

Inspecting a Stash

To see the contents of a specific stash without applying it, you can use:

git stash show -p stash@{0}  # Replace 0 with the appropriate stash index

Removing a Stash

If you want to delete a specific stash, you can do so with:

git stash drop stash@{0}  # Replace 0 with the appropriate stash index

To clear all stashes, use:

git stash clear

Try Kodaschool for free

Click below to sign up and get access to free web, android and iOs challenges.

Sign Up

3. Applying Stashed Changes Back to Your Working Directory

Once you're ready to continue working on your stashed changes, you can apply them back to your working directory.

Applying a Stash

To apply the most recent stash and keep it in the stash list, use:

git stash apply  # Applies the latest stash
git stash apply stash@{0}  # Replace 0 with the index of the stash to apply

Popping a Stash

If you want to apply the most recent stash and remove it from the list, use:

git stash pop

This command applies the stash and then removes it from the stack if there are no conflicts.

Handling Conflicts

When applying a stash, you may encounter conflicts. Git will notify you of these conflicts, and you must resolve them manually. After resolving conflicts, you can continue your work and commit the changes as usual.

Advanced Stashing Techniques

Creating a New Branch from a Stash

There may be instances where the changes in your current branch have diverged significantly from the changes saved in a stash, leading to potential conflicts when trying to reapply the stash. One effective solution is to create a new branch directly from the stash:

git stash branch <new_branch_name> <stash_id>

For example:

git stash branch test_2 stash@{0}

This command does the following:

  • Creates and checks out a new branch named test_2 based on the commit where the stash was created.
  • Applies the stashed changes to this new branch.
  • Removes the stash from the stash list.

After executing this command, you will be on the new branch with your stashed changes ready to be committed.

Stashing without Affecting the Stash Reflog

In certain situations, you might need to create a stash without modifying the stash reflog. This can be useful in scripts where stashing is used as an implementation detail and you do not want these entries to pollute the visible stash list.

To create a stash without adding it to the stash list, use:

git stash create "Description of stash"

This command returns the object name of the stash entry, which you can use later if needed. For example:

git stash create "sample stash"
# Outputs: 63a711cd3c7f8047662007490723e26ae9d4acf9

If you decide later that you want to add this stash to your stash list, you can use the git stash store command:

git stash store -m "sample stash testing" "63a711cd3c7f8047662007490723e26ae9d4acf9"

This adds the previously created stash to your stash list with a custom message, allowing you to manage it like any other stash.

Conclusion

By stashing your changes, you can keep your working directory clean and switch between tasks without losing progress. Whether you’re a novice or an experienced developer, mastering git stash is a valuable skill in your development toolkit. It can help maintain a clean and efficient workflow.

Myles Mburu

About Myles Mburu

Software Developer | AWS Solutions Architect