Use of the git stash should be the exception, not the rule. For day-to-day development, programmers should be check into topic branches, commit their code regularly and merge into a feature branch on a regular basis. But from time to time a developer needs to save a snapshot of their working tree to the stash.
But the problem with intermittent use of the git stash is that it’s easy to forget what you’ve pushed in there, and its contents can grow unwieldly over time. That’s where the git stash list and git stash show commands come in. They help you track your stash history and see which entries you should keep, and choose which entries to drop.
List git stash commit history
To view your commits to the git stash history, the default list command will suffice:
git stash list
It’s worth noting that the git stash list command takes all of the same options as git log, which means there are endless enhancements and formatting options.
For example, to display stashes attached to all branches, not just the currently active one, use the –all switch
git stash list --all
If your git stash history is long, you can choose to view an arbitrary number of the most recent entries by providing a number an option.
git stash list -3
Git stash list date ranges
If you want to search the git stash history based on a date range, you can use the git stash list –before and –after options
git stash list --before 5.days.ago git stash list --after 5.days.ago
If you want to view the diff of changes for each stash, you can use the -p option:
git stash list -p
The –stat option will show a summary of changes for each element in the git stash history
git stash list --stat
And if you want a concise listing you can use the –oneline option
git stash list --oneline
Show the git stash
One you have identified the entry in which you are interested, you likely want to see what is in that stash. This is where the git stash show command comes in. This will display a summary of file changes in the stash.
To see the difference between a stash and your local Git working tree, you can use the -p options
git stash show -p stash@
To see the difference between what’s in the stash and what’s checked into the HEAD on the master branch, you can use git diff:
git diff stash@ master
Once you have found the stash of interest, you need to figure out what to do with it. The most likely course of action is to perform either a git stash push or pop.
If a git stash conflict occurs on a push or pop, you might want to instead create a new, independent branch for the stash and then merge it into your topic branch or feature branch after you have examined it. To create a branch based on a stash use the following command:
git stash branch branchname stash@1
Finally, after working with the git stash list command, you might just come to the conclusion that you don’t need a particular stash. To delete a stash you use the drop command:
git stash drop
The drop command will delete stash entries one at a time. If you’re convinced you’ll never again need to show or list git stash entries, you can remove them all with the clear command:
git stash clear
And those are the ins and outs of how to list and show git stash entries and keep track of your shelved changes.