clinical clinical - 23 days ago 5
Git Question

How to compare two git branches and filter the differences by commit message?

I have a release branch named

release/X.X.X.X
which contains all feature branches I want to deploy to production. The release branch is made on top of
master
which is the current state of production.

On every release day I make sure our release branch contains only those changes planned for the release. I use this command to compare the release and master branch:
git log release/X.X.X.X ^master --no-merges
. I then manually check the commits for keywords like "SHR-1234" which represent ticket numbers in our ticket management system. I need to compare each commit with a list of ticket numbers to identify unwanted changes.

How can I filter commits that are returned by
git log release/X.X.X.X ^master --no-merges
and do not contain keywords like "SHR-1234"? This way I can identify the ticket number of unwanted changes.

I tried grep and awk but the results are not useful because they don't filter out the whole commit.

Answer

The git log command provides two interesting options here:

--grep=<pattern>
       Limit the commits output to ones with log message that matches the specified pattern (regular expression). With more than one --grep=<pattern>, commits whose message matches any of the given patterns are chosen (but see --all-match).

       When --show-notes is in effect, the message from the notes is matched as if it were part of the log message.

Hence --grep lets you find commits that do contain some particular string or pattern. You want commits that do not contain (any or all) strings, so we move on to:

--invert-grep
       Limit the commits output to ones with log message that do not match the pattern specified with --grep=<pattern>.

(Incidentally, note that release/X.X.X.X ^master can also be spelled master..release/X.X.X.X. There's no machine-level reason to prefer one over the other—both wind up doing exactly the same thing internally—so use whichever you find more readable.)

Comments