Patricia Sauer - Authentic Programming

The Git Stash Command

assorted animal plush toy lot

The stash command supports you in saving your local changes away and keeping track of what is contained in a specific stash entry. When using the stash command, your working directory will be reverted to match the HEAD commit.

Stashing is useful if you are working on something but need to work on something else instantly without finishing you current stuff. You can save your current stuff away and work on the urgent stuff, e.g. a bugfix. After finishing the urgent stuff you can come back to the stuff you worked on before the urgent stuff came in. You know what I’m saying? 🙂

Stashing the whole working directory

In Git, you can stash the changes of your whole working directory by using the Git stash command. When no arguments are provided, Git stash is equivalent to Git stash push.

git stash

In this case the modified states for all files will be saved in the stash. The states of all files will then be rolled back to the state in HEAD.

Stashing a specific directory or file

In case that you don’t want to stash the whole working directory but a specific sub-directory, you can use the following stash command:

git stash push -- path/to/folder/or/file

By doing so, only the states of the files in path/to/folder will be contained in the stash entry and only their states will be rolled back to the state in HEAD. All other files will keep the changes you made.

Stashing with a description

After stashing for a while without cleaning up by dropping stashes which you don’t need anymore, it can get messy. It will then be hard for you to keep track of your stashes and which stash contains what. To get a cleaner list of stashes you can give your stashes a description to memorize what changes were contained in the stash:

git stash -m "Did this and that"

After that, you can have a look at your stashes by using the Git stash list command:

git stash list

The Git stash list command will result in the following:

stash@{0}: On master: Did this and that

How to apply a stashed state

To

  • remove a stashed state from the stash list and
  • apply a stashed state on top of the current working tree state,

you need to use the Git stash pop command.

If you use it like the following, the latest stash reference (i.e. stash{0}) will be assumed to be used:

git stash pop

You can even specify a stash reference:

git stash pop stash{1}

In case that you don’t want the stashed state to be removed from the stash list, you can use the Git stash apply command instead of the Git stash pop command.

git stash apply stash{1}

Resolving conflicts during applying a stash

Conflicts can occur during applying a stash. In this case, you need to resolve the conflicts and then call the Git stash drop command.

If you don’t specify a stash reference, the latest stash reference (i.e. stash{0}) will be assumed to be used:

git stash drop

You can even specify a stash reference:

git stash drop stash{1}

Listing your stash entries

You can list your stash entries by using the Git stash list command:

git stash list

Your stash entries will be listed in the following format:

stash@{n}: WIP on branchname: description

Here, n is the stash index (0 is the latest stash entry), branchname is the name of the branch which was the current branch when the stash was done, and description is a short description of the commit the entry was based on. An example of the result of the Git stash list command is the following (taken from the Git stash documentation):

stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
stash@{1}: On master: 9cc0589... Add git-stash

In this example, stash@{0} and stash@{1} are the names of the stash entries. stash@{0} is the latest entry, stash@{1} is the one before, and so on. The names of the branches which were the current branches when the stashes where created are "submit" and "master". After the names of the branches the commit of which the stash entry was based, follows.

Showing which files are contained in a stash

You can list the files contained in a stash by using the Git stash show command followed by the stash you want to inspect, e.g.:

git stash show stash@{0}

A possible output could look like this:

build.gradle                       | 9 +++++++++
src/main/resources/application.yml | 2 +-
2 files changed, 10 insertions(+), 1 deletion(-)

Showing the diff of the files contained in a stash

You can show the diff of the files contained in a stash by using the Git stash show command with the -p option, e.g.:

git stash show -p stash@{0}

How to drop a stash

In case that you don’t need a stash anymore, you can drop it by using the Git drop command:

git stash drop stash@{1}

This will then result in something like this:

Dropped stash@{1} (6ba3282885d1295ad186923becdccd20c38fa286)

Tip

Stashes may also be referenced by specifying just the stash index (e.g. the integer n is equivalent to stash{n}), e.g.:

git stash show -p 0

Reference

For full reference see Git stash documentation.

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer