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
1
  • This question shows how to do this with GNU diff, but git diff doesn’t support the relevant options. Commented Jun 15, 2018 at 10:52

2 Answers 2

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.

3

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

git diff HEAD^ HEAD --unified=0 | tail +6 | sed -e 's/^\+//'
4
  • Should't it be -6. +6 gives: tail: cannot open '+6' for reading: No such file or directory.
    – pevik
    Commented Dec 8, 2022 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". Commented Dec 8, 2022 at 22:37
  • 1
    You have POSIX definition of tail at pubs.opengroup.org/onlinepubs/9699919799/utilities/… . In theory the correct writing should be -n +6. Commented Dec 8, 2022 at 22:41
  • Sure, -n +6 works for various implementations (coreutils, busybox). But plain +6 works on busybox, but not on coreutils implementation.
    – pevik
    Commented Dec 9, 2022 at 9:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .