0

How do you scan for patterns on changed lines within a git repo?

I'm trying to find the pattern "<<<<<<<" in all files locally modified.

To do this I thought I'd output git's diff, which should show all changed lines, and then just do a grep, like:

git diff --diff-filter=d origin/master | grep -B 5 -A 5 '<<<<<<<'

However, instead of outputting a diff, this launches my meld (on Ubuntu), my local git difftool. How do I stop it from launching meld and make it simply output a diff?

Cerin
  • 60,957
  • 96
  • 316
  • 522
  • When I try git diff --diff-filter=d origin/master | findstr "\-" on windows it works just fine, printing lines that were removed. Maybe it's a linux configuration when executing git? – James Braz Dec 07 '22 at 01:34
  • @james-braz The command that's launching meld is the `git diff...` part, not the grep part. Replacing grep with findstr results in the same behavior. – Cerin Dec 07 '22 at 02:25
  • 1
    So you want to use regular git diff even though you configured [diff] tool = meld in your .gitconfig? – James Braz Dec 07 '22 at 02:41
  • There are two separate questions here once you break this down: (1) how to inspect just changed lines; (2) why does `git diff ... | ...` still run the configured difftool? The answer to (2) should be obvious now. The answer I would use for (1) is to use `git log -S` or `git log -G`. – torek Dec 07 '22 at 07:20
  • @JamesBraz I want to use the plaintext diff functionality for this specific case without disabling my normal diff tool, yes. – Cerin Dec 07 '22 at 16:22
  • @torek I want to look at local changes, even those that haven't been committed. `git log` will only show committed changes, right? – Cerin Dec 07 '22 at 16:25
  • According to this https://stackoverflow.com/questions/34119866/setting-up-and-using-meld-as-your-git-difftool-and-mergetool, meld should only be launched when using git difftool command (https://git-scm.com/docs/git-difftool), not the regular git diff command. – James Braz Dec 07 '22 at 17:00
  • Ah, yes, `git log` can't look at uncommitted data. And @JamesBraz's update is right, `git diff` shouldn't run the diff *tool* by default. However, if you have a diff filter defined via `.gitattributes`, that might explain it. – torek Dec 08 '22 at 02:38
  • @JamesBraz That's what I thought too, but unfortunately, `git diff` does launch meld. – Cerin Dec 08 '22 at 15:19

0 Answers0