Git: how to use stash

Sometimes I have a situation that I am working on some feature on my own branch and suddenly someone comes to me and says that something really important has to be fixed or improved on the main branch. Usually it happens when I am in the middle of very important changes which are not ready to be committed for some reason.

Normally, I would have to save the changes (diff) into some file, switch to the main branch abandoning any changes, apply the fix or improvement and commit it. Then I could switch back to my own branch, apply the changes (patch) from the file and continue the work. While it is not something difficult, it can be done much easier with Git.

Basic stash usage

In Git you can store your uncommitted changes into special buffer (let’s call it stash) and at the same time undo them on the working copy:

$ git stash save [optional_message]
Saved working directory and index state WIP on master: f3b0924 Fixed title of book.
HEAD is now at f3b0924 Fixed title of book.

After this the working copy will be clean and you can easily switch to a different branch and apply the fix. You can make command shorter by omitting optional message and save command because it is assumed by default.

Once you are done with bug fixing, you can switch back to the original branch and apply the saved changes again:

$ git stash pop

It will remove the last saved changes from the stash and apply them back into your working copy. After this you can continue the work as if nothing happened.

That’s all what you will need in most cases but if you want more control read on.

Advanced stash usage

Actually, stash works almost like stack. Whenever you save your changes into stash, they are put at the top of the stack and whenever you pop them, the ones from the top of the stack are removed and applied into your working copy. If you want, you can list the contents of the stack:

$ git stash list
stash@{0}: WIP on master: f3b0924 Fixed title of book.
stash@{1}: WIP on master: f3b0924 Fixed title of book.
stash@{2}: WIP on master: f3b0924 Fixed title of book.

The latest stash entries are at the top of the list. If you forget what given stash entry contains, you can peek its contents:

$ git show 'stash@{0}'

You may also pass -p option to see full diff.

I wrote that stash is almost like a stack. It is because you are not forced to apply only the latest entry but you can apply any entry even if it is in the middle of the stack. You just have to pass its name to apply subcommand:

$ git stash apply 'stash@{1}'

If you apply it this way, it will not be removed from the list and will have to be removed manually:

$ git stash drop 'stash@{1}'

You may also run:

$ git stash pop 'stash@{1}'

which will apply the changes and then immediately drop them.

Finally, it is also possible to remove all the stash entries if you don’t need them any more:

$ git stash clear

Selecting files to stash

Command git stash save (or in short git stash) stores only the modifications made to the files tracked by Git in the stash and leaves untracked files intact. This is usually what you expect but if you want you can also include untracked files into stash by adding -u option:

$ git stash save -u [optional_message]

Creating branch from stash

At some point you may decide that stash is not enough for your needs and you may want to create a new branch from it. It is very easy:

$ git stash branch <new-branch-name> [stash-id]

The command above creates a new branch named new-branch-name, checks it out and applies the changes from specified stash to it. If the stash was not specified, the latest one is used.

As a good rule of thumb, if your stash is older than few hours and you may still need it in the future, you should create a branch from it. It is because the entries in stash are very volatile while the branches are persistent and can be also pushed into remote repository.

Conclusion

Although stash functionality is nothing advanced or complicated, it is very convenient and useful when working on multiple branches and switching between them often. If you need more information, you can always check out git-stash manual page.

Advertisement

About Robert Piasecki

Husband and father, Java software developer, Linux and open-source fan.
This entry was posted in Git, Version control and tagged , , , . Bookmark the permalink.

10 Responses to Git: how to use stash

  1. I think it’s worth emphasising that stash stores just your changes and not a snapshot of the entire tree.
    This means that you can create your stash on one branch and then pop it onto another branch. When you pop from the stash a merge is done including any conflict resolution that may be necessary.

    • Jochen says:

      That’s useful, but also dangerous, because it can happen that you pop a stash on a different branch by accident.

  2. Maxime says:

    Good tips. It’s also worth mentioning that if you have a stash lying around for a while but still needed, it’s possible to create a branch from it. Stashes are meant to be temporary. They’re also never accessible remotely while branches are.

  3. Jason Yin says:

    I also found “git stash -u” very help. It includes the untracked(new) files.

  4. ericjmritz says:

    Nice introductory article. Just recently I’ve been using `git stash save –keep-index` more often [for testing](http://ericjmritz.name/2014/02/20/git-testing-patches-in-pieces/).

  5. Pingback: Weekly Reading List | Robert Cina Online

  6. Pingback: Git Stash | Irreal

  7. Xah Lee says:

    great simple tutorial. Thanks. Always nice to learn by small bits.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.