Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep the git diff?

Tags:

git

grep

diff

Is there a way to show the git-diff filtered by a given pattern.

Something like

git grepdiff pattern  changed file +++ some sentence with pattern changed file 2 --- some other pattern 

Unfortunately the simplest solution is not good enough

git diff | grep pattern   +++ some sentence with pattern --- some other pattern # not an option as doesn't put the filename close to the match 

I came with a workaround using awk

git diff | awk "/\+\+\+/{f = \$2}; /PATTERN/ {print f \$0} " 

But would love to find out that there is a command for this.

like image 652
Kuba Avatar asked Sep 17 '12 15:09

Kuba


People also ask

How do I see all git diff?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged .

What is the command to check diff in git?

The git diff command allows us to compare different versions of branches and repository. To get the difference between branches, run the git diff command as follows: $ git diff <branch 1> < branch 2>

What is git grep?

`git grep` command is used to search in the checkout branch and local files. But if the user is searching the content in one branch, but the content is stored in another branch of the repository, then he/she will not get the searching output.


2 Answers

Not sure but isn't git diff -G <regex> flag OK?

-G < regex>

Look for differences whose added or removed line matches the given <regex>. 
like image 105
CharlesB Avatar answered Oct 13 '22 20:10

CharlesB


Have you tried git diff -S<string> or git diff -G".*string.*"? Note that they are not equivalent, see the documentation about pickaxe for what -S does.

like image 26
robinst Avatar answered Oct 13 '22 19:10

robinst