Code

A Guide to Git Stash: What It Does and How to Use It

A Guide to Git Stash: What It Does and How to Use It

How to quickly make money in IT in 2025 Try yourself in different areas of IT and take the first step towards a new profession remotely!

Find out more

The git stash command is an important tool for working with version control systems. It allows you to temporarily save changes in your working directory without having to commit them to the repository. This is especially useful when you need to switch to another branch or perform other tasks without losing your current work. Git stash not only saves changes but also restores them, allowing you to easily manage multiple tasks simultaneously. Using this command will help you increase your efficiency, organize your development process, and avoid the hassle of unfinished changes. Use git stash in your practice today to make project management more flexible and convenient.

Why "put aside" code?

Imagine sitting down at your computer in the morning and finding a new message: "Hey, Andrey! Can you fix the bug with the button on the main page? All the details are already in the task. It is advisable to complete the work by this evening." You begin working on a task.

While testing hypotheses in the code, a new task arises: an urgent server bug preventing the website's payment system from functioning needs to be fixed. You are informed that you must immediately focus on fixing this issue. However, you are already close to completing the current task and do not want to lose all the written code.

Some urgent tasks may actually be urgent Illustration: Olya Ezhak for Skillbox Media

There are several ways to solve this problem. One option is to create a new branch for all changes and commit them. However, this can result in the project repository having numerous branches with obscure names like TEST 1, BUTTON FIX, or "DO NOT TOUCH!!!" This can turn the repository into an unmanageable mess, making it difficult to maintain. To avoid this, it is recommended to stick to a clear branch naming system and regularly remove obsolete ones. This will help maintain order and facilitate the development process.

A second solution is to copy all the files where changes were made and rename them so that you can easily navigate, for example, by adding the suffix "_BUG-FIX" to the file name. However, this method requires repeating these steps for each file, which makes it quite inconvenient. For this reason, this option is also not optimal.

Often, the simplest solution is not the best. Illustration: Olya Ezhak for Skillbox Media

To improve the usability of working with Git, the git stash command was developed. It automates the process of temporarily storing changes in your working directory, which simplifies code management and increases development efficiency. With git stash, you can save unfinished changes and return to them later without disrupting your current workflow. This is especially useful when you need to switch between branches or when working on several tasks at once.

What does git stash do?

The git stash command makes managing changes to your code much easier. It allows you to quickly save all your current edits to an archive, hiding them so you can switch to another branch and continue working without the burden of unsaved changes. This is especially useful when you need to temporarily put one task aside and move on to another without losing your progress. Using git stash helps keep your project organized and makes the development process more efficient.

Changes are not committed to the repository, but saved in a local repository on your computer. This git stash feature allows you to temporarily set aside changes without committing them to the commit history. It is useful when you need to switch between branches without losing your current progress. Let's return to our example. We realized that we need to save all the changes and move on to fixing another urgent bug. Let's get started. The code is not yet complete, so committing it to the master branch does not make sense. In this situation, you need to save the changes locally. To do this, we use the git stash save command. This command allows you to temporarily save changes in the working directory and restore a clean state of the repository. This is convenient when you need to switch to another branch or perform other actions without losing your current work. After you finish working with the code, you can restore the saved changes using the git stash apply command. Initially, changes were made to the index.html and style.css files. After running the git stash save command, all these changes are temporarily lost, but this state is not permanent. You can always restore them in the future.

Now all changes are saved to the local repository, creating a project state similar to what it was before you started fixing errors. This local archive can store an unlimited number of saves. All of them will be presented in a list that functions on a first-in, first-out basis. This provides convenient access to previous versions of the project and allows you to quickly restore its state if necessary.

Local storage is like a trash bin: you only see what's on top. And if you need something below, you'll have to search. Illustration: Olya Ezhak for Skillbox Media

We can create multiple saves in the repository, but how do we then determine which save we need to retrieve? It's important to be able to label all these saves. To do this, simply specify the save name in quotation marks. This will make it easy to identify and manage the saved data, which will greatly simplify the process of finding and using it in the future.

While working on a project, you may need to temporarily hide changes, including new files that were not initially added to the repository. For this, use the git stash save command with the include-untracked parameter or the shortened version -u. This command saves the current state of the working directory and the index, including all untracked files, which simplifies managing changes to the project without having to commit them. Using this command helps maintain order in the repository and facilitates further work with the code.

To add all ignored files to your local repository, use the —all or -a option. This option allows you to include files that are normally ignored by version control systems in your commit. This is especially useful when you need to preserve all changes, including temporary and configuration files that might be excluded from the normal commit process. Using this option ensures a complete archive of the project, including all aspects of its structure.

There is a more advanced method for adding individual files to Git using the git stash command and the —patch (or -p) option. When using this option, Git sequentially presents you with all files with changes and asks whether to add them to your local repository. This approach allows you to more precisely control the content you want to keep and avoid unnecessary changes in your working directory. Using git stash with the —patch— option is a convenient tool for developers, allowing them to select only the necessary changes and keep their commit history clean.

To decipher the meaning of all these letters when selecting, just type ? in the search field, and the necessary help will appear. This will allow you to quickly get information and make an informed choice.

The main available options in this situation are «y» and «n». Selecting «y» means that the file will be deferred, while «n» indicates that the file will not be deferred.

Now that we have mastered saving changes locally, it is time to learn how to view already saved data in the local repository.

We have started an urgent task and completed it successfully. Now it is time to return to our changes. Before continuing, we need to check what exactly we've saved to our local repository. To do this, we'll use the git stash list command. This command provides a list of all saved changes, allowing us to effectively manage and restore our edits.

The Git stash command lists all stashes and their descriptions, which are specified in quotation marks after the git stash save command. In this list, we can also see a stash named "WIP on main: 4532d67 our new homepage." This name was assigned by default because we didn't specify it in quotation marks. Using descriptions for stashes helps us better navigate the history of changes and manage the state of the project.

Now let's take a closer look at the contents of our stashes and then apply them to the project. To do this, we'll use the git stash show command. This command lets us review the changes that have been saved to our stash, giving us a chance to evaluate what we've saved before continuing.

This stash contains two changes to index.html and four changes to style.css. These changes are from work we were working on before something more urgent needed our attention.

When running this command, we're accessing the top-most stash, "stash@{0}: «Bug Fix: Main page»". This is the default because it has index 0. If you want to use a different stash, you'll need to append the index to the command, using stash@{stash_index}.

This document contains similar changes, but they're actually different local versions. What could have been implemented instead:

It's unclear what changes were made to this stash. Using the —patch or -p option is recommended to resolve this ambiguity. This option allows you to see the differences between your files and the changes you've made.

Now that the necessary changes have been identified, it's time to apply them to the project. To do this, use the git stash apply command, which will retrieve the last saved state from the local repository. This command allows you to effectively manage changes to your code by preserving intermediate versions and providing the ability to revert to them if necessary. Using the git stash apply command helps keep your project organized and simplifies the development process.

If you need to use an alternative stash method, specify it after the word "apply."

After executing the git stash apply command, your changes remain in your local archive. To retrieve a stash and completely remove it from the repository, use the git stash pop command. This command not only restores your changes, but also clears them from your stash list, allowing you to keep your repository tidy. Using git stash pop is especially useful when working with multiple branches, as it helps avoid confusion and makes change management easier.

When viewing your stash list, you'll notice that the last entry is missing.

To remove a specific stash, specify it after the pop command.

If you want to create a new branch in your repository with all the changes from your local repository, use the git stash branch command. This command allows you to preserve your current changes and create a new branch at the same time, simplifying the process of working with changes and providing a cleaner approach to version control. After running this command, you can continue working on the new branch, preserving all your unfinished changes. This is especially useful for keeping your repository tidy and minimizing merge conflicts.

We've successfully created a new branch and transferred all the necessary changes from your local repository to it. This also removes the data from the repository itself.

To free up space in your local repository and remove unnecessary old stashes, you can use the git stash drop command. This command allows you to remove a specific stashed item from your stash list, which helps keep things tidy and optimizes space usage. If you want to remove all stashes, use the git stash clear command. Proper stash management in git will help avoid confusion and ensure more efficient work with projects.

We've finished working on the project for today and successfully pushed all commits. However, your local stash list still contains items. It's time to clear it completely. To do this, use the git stash clear command, which will remove all stashed states, allowing you to start with a clean slate. This will help keep your repository organized and avoid confusion in the future.

Things to remember

  • git stash is a command that allows you to write code changes to a local repository and return to them later. And all this happens on your computer, not in the repository.
  • You can put as many entries as you like in this repository. You can also perform various actions on them, such as adding, deleting, or viewing their contents.
  • The basic commands for git stash are git stash save, git stash list, and git stash apply. They will help you save changes to the repository, view a list of available stashes, and apply them to your project.

Learn more about coding and programming in our Telegram channel. Subscribe to stay up-to-date with the latest news and helpful tips!

Also read:

  • What is Git: explained with diagrams
  • Test. Curse like Linus Torvalds
  • GitHub: What is it and how to use it - a guide for beginners