6

Let's say I added two lines to the file hello.rb.

# this is a comment
puts "hello world"

If I do git diff, it will show that I added two lines.

I don't want git to show any line which is a Ruby comment. I tried using git diff -G <regular expression>, but it didn't work for me. How do I do git diff so that it won't show any Ruby comments?

4
  • 4
    Probably not ideas, but for you could do git diff | grep -v "^+\s*#" depending on your use case. Commented Sep 11, 2013 at 6:41
  • 2
    There's nothing built-in to Git that will do this. It's somewhat complex, as comments are different in every language, and determining whether or not a line is really a comment or just looks like one is even more difficult without parsing files. This question has been asked on Stack Overflow a few different times, but I don't want to link to any of them because I don't see any satisfactory answers. Commented Sep 11, 2013 at 6:44
  • 1
    @JimStewart although tweaking stackoverflow.com/a/8291327/6309 would be a good start. Commented Sep 11, 2013 at 6:50
  • git -G matches regexp to show a whole commit, try a trick: git diff | grep "^[ +-][^#]" | less Commented Nov 27, 2013 at 8:52

2 Answers 2

5

One possibility would be (ab)using git's textconv filters which are applied before the diff is created, so you can even transform binary formats to get a human-readable diff.

You can use any script that reads from a file and writes to stdout, such as this one which strips all lines starting with #:

#!/bin/sh
grep -v "^\s*#" "$1" || test $? = 1

(test $? = 1 corrects the exit code of grep, see https://stackoverflow.com/a/49627999/4085967 for details.)

For C, there is a sed script to remove comments, which I will use in the following example. Download the script:

cd ~/install
wget http://sed.sourceforge.net/grabbag/scripts/remccoms3.sed
chmod +x remccoms3.sed

Add it to your ~/.gitconfig:

[diff "strip-comments"]
         textconv=~/install/remccoms3.sed

Add it to the repository's .gitattributes to enable it for certain filetypes:

*.cpp diff=strip-comments
*.c diff=strip-comments
*.h diff=strip-comments

The main downside is that this will be always enabled by default, you can disable it with --no-textconv.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This is awesome! The only thing I had to tweak was the path to sed in that script. It is at /usr/bin on macOS
3

Git cares a lot about the data that you give to it, and tries really hard not to lose any information. For Git, it doesn't make sense to treat some lines as if they haven't changed if they had.

I think that the only reasonable way to do this is to post-process Git output, as Dogbert had already shown in his comment.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.