Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to search through Git Stash items?

Tags:

git

search

So I'm learning about using Git Stash this week and figured out all of theses stashes have been accumulating on my system. I've misplaced some code and I now have a dozen stashes of code 0-11.

Is there a way that I can search through these stashes for a string value in the files within the stash to find the code I'm looking for? or Do I just have to go through and reapply each stash to search/look in them for the code I'm trying to find?


2021/03/18: Just an update from other information I have found that sort of relates.

You can create a git alias to search all your branches.

Modify you .gitconfig file, and just call by 'git stash-search <pattern>'

[alias]   stash-search = "!f() { git show $(git stash list | cut -d\":\" -f 1) | grep \"$@\" ; }; f"  
like image 776
Elijah Avatar asked Mar 02 '17 12:03

Elijah


People also ask

How do I look inside a git stash?

Viewing Your Git Stashes in GitKrakenClicking on the stash icon from the graph will allow you to see its saved contents in the commit panel on the right. You can also quickly see a list of your stashes in the left panel.

What can you do with git stash?

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.


1 Answers

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( 
like image 101
Joshua Goldberg Avatar answered Sep 23 '22 11:09

Joshua Goldberg