Git Stash - How to

With Git stash, you can save changes without committing to them immediately. Multiple stashes are also possible in one project. Find out more how to save and retrieve changes to a project using Git stash.

$1 Domain Names – Grab your favorite one
  • Simple registration
  • Premium TLDs at great prices
  • 24/7 personal consultant included
  • Free privacy protection for eligible domains

What is Git stash?

Normally, the way Git works by committing the changes you’ve made to your local repository after testing them carefully. Unfortunately, work doesn’t always happen in such a linear fashion. In some circumstances, you may be working on a site, but then need to move to another branch before you could complete the function from the first branch. In this case, committing to your changes would not be the right decision at this time because as unfinished changes would be added prematurely. This runs the risk of making things a little messy. The solution is Git stash. The command saves your started changes and restores them for you later.

This includes all changes that you have not committed to yet. If you then want to go back to those files, simply retrieve them from the git stash and finish the changes at your leisure. This way you don’t lose progress because you can save your work, and at the same time have the freedom to flexibly continue working on other things. Without Git stash, you would either get an error message on a Git Checkout or even lose the important changes you’ve made.

Syntax and functionality of Git stash

So, Git stash acts as a kind of cache for your current branch. The basic syntax looks like this:

git stash

We will show you how Git stash works step by step with a simple example. Imagine that you are working on two branches. You are currently making changes to the first branch, but then your team informs you that something also needs to be changed on the second branch. To use Git stash, do the following:

  1. Save your changes to the first branch.
  2. Use the git stash command.
  3. Switch to the second branch and make the necessary changes there.
  4. Commit your changes to the second branch.
  5. Switch back to the first branch.
  6. Use the “git stash pop” command to recall the saved progress.

Additional options using Git stash

Besides the variant explained above, Git stash boasts a few more options. The most important ones are as follows:

git stash -u Takes into account files that were not tracked.
git stash -a Takes into account all files that were ignored or not tracked.
git stash -p Allows you to select and stash only certain files.

List stashes

The use of Git stash is not limited. So, you can also use the command several times and in doing so, store several storage steps. Thus, you can list your Git stashes and get a good overview of your stored stashes. This is the command:

git stash list

The entries are then displayed in reverse chronological order. This means that the current Git stash appears first.

Normally, the individual stashes are simply shown as “WIP” (work in progress). Since this can quickly become confusing when there are multiple Git stashes, it is recommended that you add a description to the individual stashes. This is the corresponding command:

git stash save “Your description goes here”

Git Stash pop and apply

With the command “git stash pop” the last Git Stash is selected and displayed again. However, if you want to revert to another stash, you can express this with an argument. For example, if you want the second Git Stash to be displayed, set the value to 1 (since it counts from zero). This looks like this:

git stash pop stash@{1}

An alternative to “pop” is “apply”. The difference is that “pop” deletes the respective Git Stash from memory and puts it back into the current branch. And “apply” keeps the stash in memory.

Clean up Git Stash

Having the option to create multiple Git stashes makes it especially important to clean up the Git Stash in the meantime so that it doesn’t get too cluttered. There are two commands you can use for this as well:

git stash clear

Use this command to empty the entire Git Stash.

If you want to remove only a specific stash from the list, this is the appropriate command:

git stash drop <stash_id></stash_id>

Show Git Stashes

You can get the summary of your Git Stash with the following command:

git stash show

For additional insight into the full diff, use this command:

git stash show -p

A helpful tool for working with Git

Git Stash makes your work on a project easier and ensures that even unfinished changes are not lost. In our article about Git commands, you can find more useful commands. If you’re a regular user of Git, our Git Cheat Sheet including a PDF download will probably come in handy.

Tip

Deploy Now by IONOS is a fantastic solution for you to build and deploy websites and apps! Benefit from a fast setup, optimized workflows and maximum scalability. You’ll have the choice between different tariffs depending on what you need.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top