Git Stash
I’ve found the git stash
command to be most helpful during my regular working days.
These are summary of some of the ways I use it.
Stash your Work in Progress (WIP)
The simplest way to use it is to just run git stash
. This will take all your changes and apply it to the stash stack.
Applying the WIP from the Stash
If you want to just get that stash back, just run git stash pop
.
This will take the most recent items from the stash stack, put them on the working tree, then remove that item from the stack.
If it fails, it will tell you.
Advanced Stash Topics
Messages
If I know I’m not going to get back to the work in progress stuff for a little bit of time, it is nice to add a message to what I’m working on.
Running git stash push -m "Some details about the changes"
is good enough to give myself a leg up when I get back to this work.
If I’m working on a ticket or bug, I typically put the Ticket ID into the message to ensure I know it goes with what I’m working on. If I follow Issue Driven Development, then I should ideally put a note into my ticket that I’m stopping process and I’ve created a stash with the work in progress thoughts.
Reviewing the Stack
If you find yourself working with multiple stashes when you try to move from a branch to another branch or a feature or bug and around again, you can use git stash list
to see all of the items that are on the stash stack.
If you combine listing the stack with the message command, then you can easily review what you have in your stash stack.
If you forgot to update the message, it is a little bit more of a workaround to get this up and going.
For this you will have to run something like git stash push -m "$MESSAGE" stash@{$NUMBER}
, then drop the old stash with git stash drop stash@{$NUMBER}
.
This is very roundabout, so make sure you use this with caution.
Stash Differences
I’m always questioning what I’ve put in the stash and don’t want to blanket apply them, so I’ll typically use the git stash show
command.
In this case we can run with the base method to just show the files and number of lines changed.
If you run this with the patch option git stash show --patch
, you will get differential information show lines changed.
Note that the above commands only work with the latest commit.
If you want to older changes, you need to specify the stash item with git stash show stash@{$NUMBER}
.