Git diff show only lines that have been changed

When I execute diff git, it shows the lines that were added:

+ this line is added 

which were deleted:

 - this line is removed 

but also shows many lines that do not change:

 this line is not modified this line is also not modified 

As a result, the actual git diff looks something like this:

 + this line is added this line is not modified - this line is removed this line is not modified 

Can I ask git to show only those lines that have been changed and ignore all other codes that have not been changed? I wrote a method that will delete all lines that do not have a β€œ+” or β€œ-” sign in front of them, but I'm sure there should be an easier way to do this.

In my diff git, I'm only interested in string changes.

Thanks in advance.

+102
git
Sep 15 '13 at 8:55
source share
5 answers

What you want is a diff with 0 context lines. You can generate this with:

 git diff --unified=0 

or

 git diff -U0 

You can also set this as a configuration parameter for this repository:

 git config diff.context 0 

To install it globally, for any repository:

  git config --global diff.context 0 
+144
Sep 15 '13 at 9:02
source share

Another hack (on un * x) to show only lines starting with + and - :

 git diff -U0 | grep '^[+-]' | grep -Ev '^(--- a/|\+\+\+ b/)' 

The above code does the following:

  • git diff -U0 : select 0 context lines
  • The first grep includes only all lines starting with + or -
  • The second grep excludes lines starting with --- a/ or +++ b/

Note: - The above solution will need to be changed if you use additional git diff options such as -R , --src-prefix , --dst-prefix , --no-prefix , ... - Two greps can be combined into one grep -E -v '^(\+\+\+ b/|--- a/|@@ |diff --git|index )' , but I find the double grep version easier to understand.

+29
Oct 29 '14 at 3:21
source share

Following Chris’s last comment, the main problem with post-processing is that you want to save lines starting with -|+ , but you also want to filter out those that start with ---|+++ . If you save the patch files in your repo (I do, Pydoop ) on the other hand, you want to save the lines starting with --|++ , so the regex gets a little involved:

 git diff | grep -P '^\+(?:(?!\+\+))|^-(?:(?!--))' 

The regular expression uses a negative result: see Peter Boughton's answer to this question for a detailed explanation.

If you do this often, you may need to create a git alias for it:

 git config --global alias.diffonly '!git diff | grep -P "^\+(?:(?!\+\+))|^-(?:(?!--))"' 
+6
Jan 17 '14 at 10:32
source share

I think for simple cases the regex can be much shorter and easier to remember, with the caveat that this will not work if you have changes in the line where the line itself starts with + or -

 $ git diff | grep '^[+|-][^+|-]' 

The regular expression says that the line should begin with + or - , and immediately the next character should be neither one nor the other. I got the same results if I couldn’t escape + or not here, btw ...




Example:

 $ cat testfile A B C D E F G 

Say I change C to X , E to Y and G to Z

 $ git diff | grep '^[+|-][^+|-]' -C +X -E +Y -G +Z 

As I said above, this is easy for most cases. If you pass this output to a dout file, try the same regular expression, it will not work.

 $ git diff dout | grep '^[+|-][^+|-]' $ 

Anyway, hope this helps in your case

+2
Dec 07 '17 at 19:18
source share

This answer will retain the original red / green colors for readability. I have provided several syntax options:

 git diff --color | grep --color=never $'^\e\[3[12]m' git diff --color | grep --color=never $'^\033\[3[12]m' git diff --color | grep --color=never -P '^\e\[3[12]m' git diff --color | grep --color=never -P '^\033\[3[12]m' 

Explanation:

  • git diff --color necessary so that git does not turn off color when it is pumping.
  • grep --color=never designed to prevent the removal of the original color and the selection of a suitable grep string.
  • We match strings starting with red ( \e[31m ) or green ( \e[32m ) escape codes.
  • $'...' (ANSI-C citation syntax) or -P (perl syntax) allows grep interpret \e or \033 as an ESC character.
0
Aug 20 '19 at 17:49
source share



All Articles