Search code examples
gitgrepdiff

How to grep the git 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.


Solution

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