如何在Git中找到最新的非合并提交消息?

时间:2014-03-07 10:04:22

标签: git bash

我想找到一个不是合并提交的最后一个(最新的)Git提交。

(假设我们只检查提交消息,如果它不以Merge开头,那么我们假设提交不是合并 - BTW有更好的方法吗?)

我发现此帖可能有用:

http://mislav.uniqpath.com/2010/07/git-tips/

Show the last commit which message matches a regex

$ git show :/fix
# shows the last commit which has the word "fix" in its message

$ git show :/^Merge
# shows the last merge commit

我可以将这些态度混合起来以显示上次合并提交(这很有效):

$ git show --format=%B ':/^(Merge)'

然而,正则表达式的语法(从:/开始)非常模糊,不容易找到文档,我不明白如何反转它。当我尝试?!时出现错误:

$ git show --format=%B ':/^(?!Merge)'
fatal: Invalid search pattern: ^(?!Merge)

有人可以指导我如何对git show应用否定匹配吗?

请注意,提交消息可以是多行的。

编辑:

gitk merges

让我们考虑一个简单的场景,其中所有合并都可以快进(但不是)。我希望以红色标记的提交被发现为“最新”。

像这样的剧本似乎可以解决这个问题但是它很冗长

#!/bin/bash
readCommitMessage () {
    local commit="$1"
    lastCommitMsg=$(git log -1 --pretty=%B $commit)
    #echo "$lastCommitMsg"
}

commit="HEAD"
readCommitMessage
while [[ "$lastCommitMsg" == Merge* ]]; do
    commit="$commit""^2"
    readCommitMessage "$commit"
done

echo "$lastCommitMsg"

2 个答案:

答案 0 :(得分:7)

git rev-list --no-merges -n 1 HEAD

如果您想要人类可读的输出而不仅仅是在脚本编写时可能更有用的sha1,您也可以使用log代替rev-list

(如果你想要最新的日期而不是仅仅按照拓扑顺序排列的'a',你可能想要使用--date-order。)

答案 1 :(得分:0)

以下命令不如查尔斯的答案好,但短时间可以快速使用。

var result = tableA.Select(item => item.description.Split()[0]).Distinct();
相关问题