How to grep the git diff?

Kuba picture Kuba · Sep 17, 2012 · Viewed 30.7k times · Source

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.

Answer

CharlesB picture CharlesB · Sep 17, 2012

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>.