Skip to content

Git Stash

Git Stash

git stash is a handy command that helps you in cases where you might need to stash away your local changes and reset your codebase to the most recent commit in order to work on a more urgent bug/feature.

In other words, this command allows you to revert your current working directory to match the HEAD commit while keeping all the local modifications safe.

Once you are ready to get back to working on the code you had stashed away, just restore them with a single command!

Stashing Your Work

Terminal window
git stash

For example, consider a file named index.html which has been modified since the last commit.

git-stash-example

Notice that the running git status command says that there are no new changes once the git stash command is executed!

Here WIP stands for Work-In-Progress and these are used to index the various stashed copies of your work.

An important thing to keep in mind before stashing all new changes is that, by default, git stash will not stash all the untracked and ignored files. (Here, untracked files are the files that weren’t part of the last commit i.e, new files in your local repo)

In case you want to include these untracked files in the stash, you’ll need to add the -u option.

Terminal window
git stash -u

Similarly, all the files in the .gitignore file (i.e, the ignored files) will also be excluded from your stash. But you can include them by using the -a option

Terminal window
git stash -a

The following illustrations depict the behaviour of the git stash command when the above two options are included:

git-stash-options

Restoring the Stashed Changes

Terminal window
git stash apply

This command is used to reapply all the local modifications done before that copy of the work was stashed.

Note that another command that can be used to achieve this is the git stash pop command. Here popping refers to the process of removing the most recent stash content and reapplying them to your working copy.

The difference between these two commands is that the git stash pop command will remove these particular changes from the stash whereas the git stash apply command will retain those changes in the stash even after restoring them.

Consider the previous example itself, in which the file index.html was stashed. In the following image, you can see how restoring all those changes affects your local repo.

git-stash-apply

But what if you have multiple stashes and aren’t sure which one you want to start working on? This is where the next command comes into the picture!

Handling Multiple Stashed Copies of Your Work

Similar to the process involved in resetting the local repository to a particular commit, the first step involved in handling multiple stashes is to take a look at the various stashes available.

Terminal window
git stash list

This command shows an indexed list of all the available stashes along with a message corresponding to their respective recent commits.

Consider the following example wherein there are two available stashes. One, when a new script file was added and another when this script file was altered.

git-stash-2

Note that the most recent stash is always indexed as 0.

Once you know which stash you want to restore to your local codebase, the command used to restore those modifications is:

Terminal window
git stash apply n

The alternative syntax used to achieve this is as follows:

Terminal window
git stash apply "stash@{n}"

Here n is the index of the stash you want to restore.