4

I have a list of usernames that are in a text file. I update this file and commit it. I am looking for a way to get a list of the changes since the last commit.

I do not want any diff formatting at all, I just want to get an output that has usernames one per line (as they are added to each commit) since the last commit.

I can't find a setting that will remove all the git diff syntax from the output so it's purely a list new lines added only.

Example

Original file:

user1
user2
user3

I then add

user4
user5

Then commit.

I want to be able to do a git diff and see only:

user4
user5
andcoz
  • 16,830
  • 3
  • 38
  • 45
Michael
  • 141
  • 1
  • 4
  • [This question](https://unix.stackexchange.com/q/56625/86440) shows how to do this with GNU `diff`, but `git diff` doesn’t support the relevant options. – Stephen Kitt Jun 15 '18 at 10:52

2 Answers2

6

You could grep the diff output combining lookahead/lookbehind:

git diff --unified=0 | grep -Po '(?<=^\+)(?!\+\+).*'
  • (?<=^\+) is a positive lookbehind for line starting with +
  • (?!\+\+) is a negative lookahead to prevent matching files headers starting with +++ a/path/to/file.

The --unified=0 git option is just there to reduce the numbers of line to filter removing diff context, it is optional though.

There is probably better, I'm not fluent in PCRE.

kaliko
  • 553
  • 6
  • 14
3

Borrowing --unified=0 from kaiko answer I came to:

git diff HEAD^ HEAD --unified=0 | tail +6 | sed -e 's/^\+//'
Patrick Mevzek
  • 3,130
  • 2
  • 20
  • 30
  • Should't it be `-6`. `+6` gives: `tail: cannot open '+6' for reading: No such file or directory`. – pevik Dec 08 '22 at 19:21
  • @pevik Depends on your `tail` flavor... If `+6` is not ok, try `--lines=+6` but look at the specific manual of your tail command. `-6` and `+6` have a different meaning, `-6` would mean last 6 lines (because tail "starts" from the end) where `+6` means "output everything from line 6 to end". – Patrick Mevzek Dec 08 '22 at 22:37
  • 1
    You have POSIX definition of `tail` at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/tail.html#tag_20_125 . In theory the correct writing should be `-n +6`. – Patrick Mevzek Dec 08 '22 at 22:41
  • Sure, `-n +6` works for various implementations (coreutils, busybox). But plain `+6` works on busybox, but not on coreutils implementation. – pevik Dec 09 '22 at 09:13