3

all of my git commit messages start with

  refs #SOME_NUMBER

where SOME_NUMBER is a number from 1 up. I would like to parse all commmit messages on my working branch, store all of the SOME_NUMBERs in a list, remove duplicates, and save to file. Not really sure where to start....

Jacko
  • 12,665
  • 18
  • 75
  • 126

1 Answers1

7

You can do that pretty easily with this shell one-liner:

$ git log --format=%s | cut -f 2 -d ' ' | sed 's/#\(.*\)/\1/' | sort -n | uniq > refs.txt

Explanation:

  1. git log --format=%s displays the first line of every commit message
  2. cut -f 2 -d ' ' splits the line by a space, and prints the second part of the (the #SOME_NUMBER portion)
  3. sed 's/#\(.*\)/\1/' removes the number sign from the number
  4. sort sorts the entries in ascending numerical order
  5. uniq ensures that each number is only printed once
  6. > refs.txt prints the output to a file called refs.txt.
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • To sort numbers, use "sort -n". Also uniq is not reliable unless sorted first; it only removes consecutive duplicates, so it is not just a "nice touch", but required. – Peter Apr 24 '12 at 09:51