Signup/Sign In

Git Stash

The meaning of the word stash is to safely store something in a hidden place. This definition completely describes what Git Stash does. It stores changes in our project in a safe place so that we can work on something else. We can come back to the stashed changes and continue our work from where we left it. Let’s learn more about stashing in Git.


  • Stashing is the process of storing unsaved changes of our project in a separate location. After stashing, all the changes that were not yet committed are temporarily stored.

  • Stashing stores both staged and unstaged files. But untracked files are not stashed by default and we need to specify if we want to stash these files. Untracked files are files that are newly created and not added to the staging area even once. Unstaged files are files that were added to the staging area at least once but have been modified.

  • Our project is rolled back to the last committed state but we still have our work safely stashed away.

  • Stashing is also independent of branches. What it means is that we can stash the changes we made in one branch and we can continue to work on those changes in some other branch by “un-stashing” in that branch.

After running Git Stash, the uncommitted changes are stored in the stash.

Why Stashing is used?

There may be scenarios where you are working on one feature of a project but want to apply changes to another feature on some other branch. But you are not done with the feature you are currently working on and don’t want to commit it yet. You can just stash the changes and switch branches. When you come back to complete the feature you can just “un-stash” the changes and continue from where you left. Stashing is just a way of temporarily storing changes. Even though we have the option to commit the changes but it is not a wise idea to commit some unfinished work and corrupt our committed history.

Git Stash Command

The Git Stash command has several options to help us to stash and “un-stash” files. Let’s take a look at some of the most important and useful options.

Adding a Stash

To simply stash the changes:

$ git stash

We can add a message to our stashes by using the save option. This helps us better understand what was stashed. This is useful if we are frequently stashing our changes from different branches.

$ git stash save “Message”

By default, Git will not stash untracked files. But we can still stash them by using the -u flag or the --include-untracked option.

Viewing a Stash

We can use the list option to view all the saved stashes. The highlighted part in the image shows the stash id. It is of the form stash@{stash-index}

Viewing the stash id using the git stash list command

$ git stash list

We can view the summary of a stash by using the show option. This will display the files that were stashed and changes made to them. By default, it displays the last stash but we can also pass the stash id to view some other stash information.

$ git stash show

Viewing the files that were stashed using the show option

We can also view all the changes made to the files using the -p flag along with the Git Stash Show.

$ git stash show -p

Viewing the changes in the files that were stashed using the -p flag with the show option

Retrieving the Stashes

Just stashing away the changes is not enough. We should be able to get those stashed changes back. Git provides us a few different options to do that.

To get the last stashed change back we can use the apply option.

$ git stash apply

We can also get the changes saved to any previous stash by using its stash id.

$ git stash apply stash@{stash-index}

We can also apply the stash changes and delete the saved stash by using the pop option. If no stash id is provided then it will pop and apply the most recent stash.

$ git stash pop stash@{stash-index}

If you have stashed changes to a certain file and then work on that same file then conflicts may arise when you apply your stash back to that branch. We can avoid these conflicts by applying the stashed changes on a new branch using the following command.

$ git stash branch <branch-name> stash@{stash-index}

Deleting Stashes

We can also directly remove a stash without applying it by using the drop option with Git Stash:

$ git stash drop stash@{stash-index}

We can delete all the stashes with a single command by using the clear option.

$ git stash clear


There will be many situations where we are halfway through a task but want to switch to some other urgent task without committing the incomplete work. Stashing is a perfect solution to such problems. It saves the changes in a temporary location and allows us to work on something else. An alternative to stashing is by creating a new commit and then later on amending that commit. But stashing is still considered a better option. I hope this tutorial was helpful and you learned something new.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight