Is it possible to search through Git Stash items?

One-liner:

git grep whatever $(git stash list -q | cut -d":" -f 1)

and git grep conveniently outputs the the changed line with the name of the stash and the name of the file:

stash@{43}:common/ot/whatever.js:exports.whatever = (foo, deps) => {
stash@{44}:common/ot/whatever.js:exports.whatever = (foo, deps) => {

git stash list -S "my string" works on Git 2.28 to find patches that add or remove "my string".

I don't know how long this capability has been around; the documentation implies that it should accept all the same options as git log, including -S, but I distinctly remember not being able to search this way a few years ago.


git stash show -p stash@{n} | grep "john cena" is the only option I think.

Of course you can write your own script around that.


There are some helpful ideas in this gist and discussion thread

First, just listing matching stashes is easy (with or without -i, depending if case matters)

git stash list -i -G<regexp>

If there's not that much to dig through, you can just add -p to print the matching stashes in their entirety.

git stash list -i -p -G<regexp>

With more power for "real" cases, add to .gitconfig:

[alias]
    stashgrep = "!f() { for i in `git stash list --format=\"%gd\"` ; \
              do git stash show -p $i | grep -H --label=\"$i\" \"$@\" ; done ; }; f"

and then you can call git stashgrep with any grep arguments you like (-w, -i). e.g.,

git stashgrep -i <regexp>

This differs from some answers above, in that it prepends the stash ID to show you where each difference came from:

% git stashgrep -i tooltip
stash@{5}: //            resetBatchActionTooltip();
stash@{5}:         addAcceleratorsAndTooltips(lToolMenu, lToolButton, iListener, iTool);
stash@{5}:     private void addAcceleratorsAndTooltips(AbstractButton lToolMenu,
stash@{5}:+        String lToolTip = iTool.getToolTipText();
stash@{5}:             lToolButton.setToolTipText(lToolTip);
stash@{20}:+    private static final String invalidSelectionTooltip = "Invalid selection.  Please choose another.";
stash@{20}:-    private final String invalidSelectionTooltip = "Invalid selection.  Please choose another.";
stash@{20}:                         ((JTextField)lComponent).setToolTipText(

Tags:

Git

Search