如何比较两个 git 分支并通过提交消息过滤差异?

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

我有一个名为 release/X.X.X.X 的发布分支,其中包含我要部署到生产环境的所有功能分支。发布分支是在 master 之上创建的,这是当前生产状态。

在每个发布日,我确保我们的发布分支只包含为发布计划的那些更改。我使用这个命令来比较发布和主分支:git log release/X.X.X.X ^master --no-merges。然后,我手动检查提交的关键字,如 "SHR-1234",这些关键字代表我们的票务管理系统中的票号。我需要将每个提交与票号列表进行比较,以确定不需要的更改。

如何过滤由 git log release/X.X.X.X ^master --no-merges 返回且 不包含 关键字(如 "SHR-1234")的提交?这样我就可以识别不需要的更改的票号。

我尝试了 grep 和 awk,但结果没有用,因为它们没有过滤掉整个提交。

你可以使用-G'正则表达式'来满足你的要求

git log release/X.X.X.X ^master--no-merges -G ‘regular expression’(包括或排除指定的提交)

git log command 在这里提供了两个有趣的选项:

--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.

因此 --grep 可以让您找到 do 包含某些特定字符串或模式的提交。您希望提交 包含(任何或所有)字符串,因此我们继续:

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

(顺便说一句,请注意 release/X.X.X.X ^master 也可以拼写为 master..release/X.X.X.X。没有机器级别的理由偏爱其中一个——两者最终在内部做完全相同的事情——所以使用以你觉得更易读的为准。)

问题标题承认略有不同的答案,这可能对某些人有用。通过提交标题 only 比较两个分支的一种简单方法是将每个分支的提交标题转储到单独的文本文件,然后在差异查看器中打开这两个文件:

git --no-pager log --pretty=format:%s main > log_main.txt
git --no-pager log --pretty=format:%s other > log_other.txt
meld log_main.txt log_other.txt

我们首先将分支main的提交主题转储到文件log_master.txt,然后将分支other的主题提交到文件log_other.txt,并在视觉差异查看器 meld (one alternative is kdiff3).