46

I'm trying to get only new version of lines which have changed and not all the other info which git diff shows.

For:

git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix 

It shows:

diff --git file1 file2 index d9db605..a884b50 100644 --- file1 +++ file2 @@ -16 +16 @@ bla bla bla -old text +new text 

what I want to see is only:

new text 

Is it possible?

3
  • 2
    possible duplicate of Git diff to show only lines that has been modified Commented Aug 26, 2014 at 4:21
  • 2
    Nope, it is not a duplicate, @metacubed I saw the question and incorporated the answer, but it did not help completely. I was able to get rid of heading and trailing lines but not all the other information. Commented Aug 26, 2014 at 4:25
  • I added an answer based on your specific question. Commented Aug 26, 2014 at 4:39

7 Answers 7

24

If you specifically want only the new text part, then use the following:

git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep "^\+" 

This is basically your code, piped into the egrep command with a regex. The regex will filter only lines starting with a plus sign.

If you want to use this as an alias in scripting context, make sure to escape the escape char. Inside your ~/.gitconfig file, add:

[alias] diffaddedonly = !git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep '^\\+' 
Sign up to request clarification or add additional context in comments.

5 Comments

git diff HEAD^^ --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep "^\-[^\-]"
what about stripping the + sign prefix at the beginning of each line? I'm creating some crazy rebase recipes..
@DevinRhode : Add " | sed 's/\+//' " to remove the leading +
Why the separate egrep instead of using sed to filter? E.g. git diff … | sed -n '/^\+/s///p'.
@TobySpeight Thanks, I added another backslash to that and that worked for me: | sed -n '/^\\+/s///p'; }; f
22

Only added lines does not make sense in all cases. If you replaced some block of text and you happened to include a single line which was there before, git has to match and guess. Usually the output of git diff could be used as input for patch afterwards and is therefore meaningful. Only the added lines are not precisely defined as git has to guess in some cases.

If you nevertheless want it, you cannot trust a leading + sign alone (since we cannot distinguish between file header beginning +++ and an added line that happens to begin with ++). Maybe filtering all the green line is better:

git diff --color=always|perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/' 

for only deleted lines filter for all the red lines:

git diff --color=always|perl -wlne 'print $1 if /^\e\[31m-(.*)\e\[m$/' 

to inspect the color codes in the output you could use:

git diff --color=always|ruby -wne 'p $_' 

7 Comments

Why is the leading "+ " not trustworthy?
The file header starts with +++ and if you add a line with two plus signs you result in +++, too. You could solve this with more parsing logic but just taking the green lines looks easier.
Ah, you're completely right. There is no space after the "+" in the git diff output. My mistake.
Awesome! How do I show deleted versions of the file?
Use git diff --color=always|perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/'|sed -r 's/\x1B\[m\x1B\[41m[[:blank:]]+//g' to remove trailing spaces color code. If you're using mac OS, replace sed with docker run --rm -i hairyhenderson/sed.
|
9

You can use:

git diff -U0 <commit-hash> | grep "^\+\""

This will give your output as "+new text"

Comments

5

Here is an answer using grep. It retains the original red/green colors for readability. I provided a few variations in syntax:

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 is needed to prevent git from disabling the color when it is piping.
  • grep --color=never prevents grep from highlighting the matched string (which removes the original color from the original command)
  • Match lines that start with red (\e[31m) or green (\e[32m) escape codes.
  • The $'...' (ANSI-C quoting syntax) or -P (perl syntax) is to let grep to interpret \e or \033 as an ESC character.

Comments

2

Unfortunately the color stuff isn't so portable to windows (with the commands supported by unxutils). However there is a solution to the +++ problem solved by using the color mode.

Additionally we probably don't want the leading character either. So lets use sed to get rid of it once we've matches with it:

git diff --no-ext-diff --unified=0 -a --no-prefix --output-indicator-new=% | sed -n "s/^%\(.*\)$/\1/p"

if you want deleted lines:

git diff --no-ext-diff --unified=0 -a --no-prefix --output-indicator-old=% | sed -n "s/^%\(.*\)$/\1/p"

By substituting % for + you don't have to worry about the header lines that start with +++ (b/c git doesn't use that prefix there... however that could change, this is still a porcelain command)

The prefix character must be ASCII (so no getting fancy with unicode). Given the vagaries of Windows/Linux/sed syntax. I think, the only ASCII chars that won't require escaping somewhere are: %,_~

Depending on your needs you may want to drop -a as binary files can make a mess of things.

--exit-code mentioned in another answer is a noop. By running thru a pipe, we only get the second exit code (tested on both Windows and Linux)

1 Comment

Matching .* and then replacing it with \1 is a no-op - we can write 's/^%//p' instead for a simpler sed program. Note also use of single quotes where we don't need/want shell expansion.
1

If you want the process to be automatic, for a single file, you can use diff rather than git diff, doing :

diff --changed-group-format='%>' --unchanged-group-format='' <( git show HEAD:myfile.ext ) myfile.ext 
  • <( ) makes diff use the result of git show HEAD:myfile.ext as first input
  • --changed-group-format='%>' tells diff to output added lines
  • --unchanged-group-format='' tells diff to output nothing for removed lines

For getting the info for a commit (not for the current worktree modifications), you can even do

diff --changed-group-format='%>' --unchanged-group-format='' <( git show _SHA_~:myfile.ext ) <( git show _SHA_:myfile.ext ) 

where _SHA_ is any reference to a commit (SHA, branch, tag, ...)

(For those who wonder why I needed this : I've got a "update.sql" file in which all SQL statements are put. So, on project update, I've got to find which lines have been added, to make Mysql execute them).

I found these diff options in this answer

1 Comment

Doesn't work for newly added file
-1

A pragmatic approach: Restore the old version (take care not to overwrite your changes), and use good old diff.

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.