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"
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.
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.
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(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With