Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I output only new lines added in git commit id?

Tags:

git

shell

How do I output only new lines added in git commit id? I have not found the corresponding usage of git show

like image 550
daidai21 Avatar asked Mar 04 '26 21:03

daidai21


2 Answers

Filter the output by + in the first column:

git show <commit-id> | grep '^\+'
like image 158
phd Avatar answered Mar 07 '26 12:03

phd


The lines which are added can be identified by searching for + in the beginning of each line as suggested by phd's answer. However, this will also display --- a/file +++ b/file in the output.

One way is to search for the colored text. In the diff output, the lines added are represented in green color.

git show <commit-id> --color | grep ".\[32m+"

Green color is represented by this [32m in ANSI and \033[32m in ASCII where 033 is the ESC character. (ANSI codes start with ESC character or escape character)

As per hexdump, . represents the ESC character in this case.

Sample hexdump:

printf '%b\n' 'It is \033[31mnot\033[39m intelligent to use \033[32mhardcoded ANSI\033[39m codes!' | hexdump -C
00000000  49 74 20 69 73 20 1b 5b  33 31 6d 6e 6f 74 1b 5b  |It is .[31mnot.[|
00000010  33 39 6d 20 69 6e 74 65  6c 6c 69 67 65 6e 74 20  |39m intelligent |
00000020  74 6f 20 75 73 65 20 1b  5b 33 32 6d 68 61 72 64  |to use .[32mhard|
00000030  63 6f 64 65 64 20 41 4e  53 49 1b 5b 33 39 6d 20  |coded ANSI.[39m |
00000040  63 6f 64 65 73 21 0a                              |codes!.|

Refer to this for more info on the color codes in bash

Another way is to change the identifier (character +) for added lines to some other character like ~.

Using show

git show <commit-id> --unified=0 --no-prefix --color=never --output-indicator-new=~ | grep "^[~]"

Using diff

git diff HEAD <commit-id> --unified=0 --no-prefix --color=never --output-indicator-new=~ | grep "^[~]"

What each option does ?

  • unified=0 : Setting lines of context to 0

  • no-prefix : Removing source and destination prefix

  • color=never : Show non-colored diff to avoid messing up the grep part

  • output-indicator-new=~ : Change the character of added lines from + to ~

  • "^[~]" : Filter the diff output to show lines starting with ~ character

If you want to eliminate ~ from the output, then you can pipe the output to this

awk -F'^[~]' '{print $2}'
like image 25
Saurabh P Bhandari Avatar answered Mar 07 '26 11:03

Saurabh P Bhandari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!