Can I do a search through Git Stash?

So, I found out about using Git Stash this week and understood all the points that have accumulated in my system. I missed some code, and now I have a dozen stashes of code 0-11.

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

+8
source share
5 answers

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

Of course, you can write your own script around this.

+11
source

The git grep accepts a tree object:

SYNTAX

 git grep [-a | --text] [-I] [--textconv] [-i | --ignore-case] [-w | --word-regexp] [-v | --invert-match] [-h|-H] [--full-name] [-E | --extended-regexp] [-G | --basic-regexp] [-P | --perl-regexp] [-F | --fixed-strings] [-n | --line-number] [-l | --files-with-matches] [-L | --files-without-match] [(-O | --open-files-in-pager) [<pager>]] [-z | --null] [-c | --count] [--all-match] [-q | --quiet] [--max-depth <depth>] [--color[=<when>] | --no-color] [--break] [--heading] [-p | --show-function] [-A <post-context>] [-B <pre-context>] [-C <context>] [-W | --function-context] [--threads <num>] [-f <file>] [-e] <pattern> [--and|--or|--not|(|)|-e <pattern>...] [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...] [--] [<pathspec>...] 

Now consider that the stash entry is a tree object synthesized from the contents of the working tree when you called git stash with two parents, which are the state in HEAD and the state in the index; lead manual:

A bookmark is represented as a commit, whose tree entries state the work and its first parent is the commit in HEAD when the cache was created. The second parent tree records the state of the index when the stash is done and it is made a child of the HEAD commit. The ancestral graph is as follows:

  .----W / / -----H----I 

where H is a HEAD commit, I is a commit that records the state of the index, and W is a commit that records the state of the working tree.

Thus, you can have a tree for grep for your bookmark entry:

  • git grep [options] term stash@ {n} will be grep, which is W commit for term , ie it will grep the saved state of the working files of the tree.

  • To grep the index state of a hidden record, you need to specify W for the second parent; this is done using the suffix ^2 :

      git grep [options] term stash@ {n}^2 
  • In order to execute the grep state of fixing the base line, an interesting case is to contact the first parent:

      git grep [options] term stash@ {n}^1 

The notation ^<n> is explained in the git help revisions manual:

<rev>^ , for example. HEAD^ , v1.5.1^0 Suffix ^ to revise The parameter means the first parent of this commit object. ^<n> means parent <n> th (i.e., <rev>^ equivalent to <rev>^1 ). As a special rule, <rev>^0 means commit itself and is used when <rev> is the name of the tag object object that refers to the commit object.

TL DR

For top entry, use

  • git grep whatever stash@ {0} so that grep what the state of the working tree was.
  • git grep whatever stash@ {0}^2 so that grep what the state of the index was.
+4
source

Single line:

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

and git grep conveniently displays the modified line with the name stash and the file name:

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

Add an answer to Andrejs Cainikovs, you can add seq to search all of this seq 0 5 | xargs -I {} git stash show -p stash@ {\{\}} | grep your_pattern seq 0 5 | xargs -I {} git stash show -p stash@ {\{\}} | grep your_pattern

0
source

There are some helpful ideas in this gist and discussion thread in this thread and discussion.

First, just make a list of suitable stashes (with or without -i, depending on the case)

 git stash list -i -G<regexp> 

If you don’t have much to dig, you can simply add -p to print the corresponding stashs as a whole.

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

Having added more features 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 invoke git stashgrep with any grep arguments you like. eg,

 git stashgrep -i <regexp> 

This differs from some of the answers above in that it adds a stash identifier to show you where each difference came from.

0
source

Source: https://habr.com/ru/post/1015403/


All Articles