想象一下下面的历史:

       c---e---g--- feature
      /         \
-a---b---d---f---h--- master

我怎么能找到当提交“c”已合并到主(即,找到合并提交“h”)?


当前回答

我需要这样做,并以某种方式找到了git-when-merged(它实际上引用了这个SO问题,但Michael Haggerty从未在这里添加对他非常出色的Python脚本的引用)。现在我有了。

其他回答

Git-get-merge将定位并显示你正在寻找的合并提交:

pip install git-get-merge
git get-merge <SHA-1>

该命令跟随给定提交的子分支,直到发现合并到另一个分支(假设是主分支)。

对于Ruby人群来说,有git- where。非常容易。

$ gem install git-whence
$ git whence 1234567
234557 Merge pull request #203 from branch/pathway

也就是说,总结一下Gauthier的文章:

perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' <(git rev-list --ancestry-path <SHA-1_for_c>..master) <(git rev-list --first-parent <SHA-1_for_c>..master) | tail -n 1

EDIT:因为它使用进程替换“<()”,所以它不兼容POSIX,并且它可能无法与您的shell一起工作。不过,它适用于bash或zsh。

图形化的解决方案是在gitk中找到它(使用“SHA1 ID”框),然后沿着提交行直到合并提交。

我的ruby版本的@robinst的想法,工作速度快了两倍(这在搜索非常旧的提交时很重要)。

find-commit.rb

commit = ARGV[0]
master = ARGV[1] || 'origin/master'

unless commit
  puts "Usage: find-commit.rb commit [master-branch]"
  puts "Will show commit that merged <commit> into <master-branch>"
  exit 1
end

parents = `git rev-list #{commit}..#{master} --reverse --first-parent --merges`.split("\n")
ancestry = `git rev-list #{commit}..#{master} --reverse --ancestry-path --merges`.split("\n")
merge = (parents & ancestry)[0]

if merge
  system "git show #{merge}"
else
  puts "#{master} doesn't include #{commit}"
  exit 2
end

你可以这样使用它:

ruby find-commit.rb SHA master