-1

How to git grep every commit in repo to display only commits that have lines with added word.
I am looking for a word 'introduced' and I need to find a commits that have that word added, but not in the message of a commit but in file's text that has been committed.
Also, I need to use 'git grep' not grep

My question is not a duplicate as the suggested question only answers generally how to use grep and I am asking more specific, not how to use grep but how to find a specific information using grep

There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
  • 1. Post sample data. 2. Post expected results. 3. What have you tried so far? – James Brown May 05 '18 at 06:33
  • 1.Sample data: file.cpp, content: int main() {const std::string s{"introduced";}}. Expected result: only commit where this file had the "introduced" word added is listed after git grep. 3. What I've tried so far: Tried git log --all --grep (different patterns here)< – There is nothing we can do May 05 '18 at 06:42
  • 1
    Possible duplicate of [How to grep (search) committed code in the git history?](https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history) – nbari May 05 '18 at 07:22
  • @Thereisnothingwecando To search for commit content (i.e., actual lines of source, as opposed to commit messages and the like), what you need to do is: `git grep $(git rev-list --all)` what part from that is not clear for you ? – nbari May 05 '18 at 07:29
  • @nbari, the unclear part to me is how to filter out lines that had only the added word I'm looking for – There is nothing we can do May 05 '18 at 07:30
  • That's what grep is for. You can pipe the output of git grep into grep. Or you can also use less, which is more interactive. Or you can just redirect the output into a file, and use an ordinary text editor to search in the file. There are many possibilities. – Robin Green May 05 '18 at 09:48
  • @RobinGreen I know what grep is for, where did I ask, what grep is for? – There is nothing we can do May 05 '18 at 10:34
  • You wrote, and I quote " the unclear part to me is how to filter out lines that had only the added word I'm looking for". I am telling you, you can use grep for that. Please note that git grep is not the same as grep. git and grep are two separate pieces of software. – Robin Green May 05 '18 at 10:35
  • @RobinGreen I know that I can use grep for that, duhhh... this does not answer my question: use grep and giving link to another question how to use grep. And I also need to use git grep as I've stated in my OP – There is nothing we can do May 05 '18 at 10:38
  • git grep operates on blobs, not on diffs. If you want diffs (like added lines) you should use git log. `git log -S` or the like. If it is not exactly as you want, you can generate patches with `git log`, and make a script that parses them as you want. – battlmonstr May 05 '18 at 13:36

1 Answers1

0

You can use git's "pickaxe search" (I prefer that to git grep for this use case) and grep together.

git log -p --all -G regex | grep regex

This will also include removals of "regex", which is not what you asked for, but it's close to what you asked for.

If you need more context, you can supply arguments to grep to provide more context, or alternatively use less:

git log -p --all -G regex | less 

inside less you will need to press / and then type the search regex again and press Enter. That will find the first match. To find the next matches, you can just press / and Enter again.

Alternatively you can output the entire results to a file myfile:

git log -p --all -G regex > myfile

and open myfile in a text editor and search it there.

Robin Green
  • 32,079
  • 16
  • 104
  • 187