41

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?

Artem
  • 7,275
  • 15
  • 57
  • 97
  • 2
    possible duplicate of [Git diff to show only lines that has been modified](http://stackoverflow.com/questions/18810623/git-diff-to-show-only-lines-that-has-been-modified) – metacubed Aug 26 '14 at 04: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. – Artem Aug 26 '14 at 04:25
  • I added an answer based on your specific question. – metacubed Aug 26 '14 at 04:39

7 Answers7

21

Only added lines does not make sense in all cases. If you replaced some block of text and you happend 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. 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 $_'
michas
  • 25,361
  • 15
  • 76
  • 121
  • Why is the leading "+ " not trustworthy? – Martin G Aug 26 '14 at 05:56
  • 5
    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. – michas Aug 26 '14 at 06:01
  • Ah, you're completely right. There is no space after the "+" in the git diff output. My mistake. – Martin G Aug 26 '14 at 06:03
  • Awesome! How do I show deleted versions of the file? – Artem Aug 26 '14 at 06:14
  • 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`. – Kewei Shang Mar 02 '20 at 14:31
  • Why is this not working for [this commit](https://github.com/chromium/chromium/commit/104e65a2081) where the lines start with a `-` sign, but are nevertheless red if you run `git diff 104e65a2081^ 104e65a2081`? – Splines Sep 01 '21 at 13:01
20

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 '^\\+'
Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
metacubed
  • 7,031
  • 6
  • 36
  • 65
  • 1
    git diff HEAD^^ --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep "^\-[^\-]" – Artem Aug 26 '14 at 06:11
  • 1
    what about stripping the + sign prefix at the beginning of each line? I'm creating some crazy rebase recipes.. – Devin Rhode Oct 07 '22 at 03:59
9

You can use:

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

This will give your output as "+new text"

Srini
  • 91
  • 1
  • 2
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.
wisbucky
  • 33,218
  • 10
  • 150
  • 101
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

Pierre-Olivier Vares
  • 1,687
  • 15
  • 20
1

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)

DevNull
  • 131
  • 7
1

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

ynux
  • 1,280
  • 14
  • 21