Grep Jira Issue Number From Git Log Commit Message

TLDR;

git log --pretty=oneline master..your-branch | grep -e '[A-Z]\+-[0-9]\+' -o | sort -u

Grep Jira Issue Number From Git Log Commit Message

Many of my blog readers have discovered my article on how to add the Jira Issue Number to Git Commit Message and I continue to work with projects where we make use of the Atlassian toolchain, most prominently Jira for issue tracking and Confluence as a Wiki.

Assuming your commit log looks like this:

commit 8e0e17e6150c757c5ed186413fd304940435ac6e
Author: Kevin English <me@kenglish.co>
Date:   Mon Feb 12 15:57:30 2018 -0800

    [GUS-2733] Update ProductController not to use param for is_mobile

commit df6563d9f54ea11f04dbbf67f5c13642f7f77556
Author: Kevin English <me@kenglish.co>
Date:   Mon Feb 12 15:33:10 2018 -0800

    [GUS-3019] Rename SLICE_SIZE to CHARS_IN_PREVIEWS

commit 91ccb455ac88792f7d9d98b660f7254f725d61c1
Author: Kevin English <me@kenglish.co>
Date:   Mon Feb 12 15:17:17 2018 -0800

    [GUS-3019] Remove whitespace

It can be particular helpful, if you comparing between a master branch and a dev branch to discover which jira issues are resolved in dev but currently not in master. To this, we can compare the git logs between the two branches with this command:

git log master..dev

However, this gives us a long format similiar to that one above. To get the issue numbers from this output, we’d have to manually cut and paste. We’re programmers so we’re not doing that :)

Instead, let’s make the output a bit shorter:

git log --pretty=oneline  master..dev

Now we should be seeing something like this:

8e0e17 [GUS-2733] Update ProductController not to use param for is_mobile
df6563 [GUS-3019] Rename SLICE_SIZE to CHARS_IN_PREVIEWS
91ccb4 [GUS-3019] Remove whitespace

This output is easier to work with because we can pipe it into grep. We use the regex argument and have grep print out the matching text:

git log --pretty=oneline master..dev | grep -e '[A-Z]\+-[0-9]\+' -o

Now we’ve got:

GUS-2733
GUS-3019
GUS-3019

If the list is long, we probably want to sort it and remove any duplicates. To do this, we pipe again but this time into sort:

git log --pretty=oneline master..dev | grep -e '[A-Z]\+-[0-9]\+' -o | sort -u

And we’ll get:

GUS-2733
GUS-3019

That should save some time in the future!