2

If I run git diff I get this

diff --git a/README b/README index 8d78ee4..67451cd 100644 --- a/README +++ b/README @@ -1,3 +1,3 @@ dog -cat +fox bird 

However I only want this

-cat +fox 

I ended up using this

git diff --color | awk '/^\033\[3[12]m/' 
1
  • out of curiousity: why do you only want the changed lines without any context? If you have multiple changed lines, things will become complicated.... Commented Dec 21, 2013 at 10:47

3 Answers 3

4

This works on my system:

git diff --color | grep -P '\e\[3[12]m' 

It uses color output to differentiate those lines. By searching for the green and red color markers it should only print the added and removed lines.

This answer may help to understand the color aspect more if you need to modify it.

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

Comments

1

You can use the -U or --unified flag to specify how much context to show.

-U<n>, --unified=<n> Generate diffs with <n> lines of context instead of the usual three. Implies -p. 

You want -U0, or --unified=0.

As far as I know git doesn't provide a way to suppress the header.

1 Comment

That's only a partial answer. For the OP's example, that will suppress two lines out of the seven asked to suppress.
0

sed

git diff | sed '/^i/{N;N};/^[-+]/b;d' 

awk

git diff | awk '/^i/{c=NR}NR>c+2&&/^[-+]/' 

With both solutions:

  1. Filter out +++ and --- lines
  2. Filter in added and removed lines

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.