想象一下下面的历史:
c---e---g--- feature
/ \
-a---b---d---f---h--- master
我怎么能找到当提交“c”已合并到主(即,找到合并提交“h”)?
想象一下下面的历史:
c---e---g--- feature
/ \
-a---b---d---f---h--- master
我怎么能找到当提交“c”已合并到主(即,找到合并提交“h”)?
当前回答
添加到~/.gitconfig:
[alias]
find-merge = "!sh -c 'commit=$0 && branch=${1:-HEAD} && (git rev-list $commit..$branch --ancestry-path | cat -n; git rev-list $commit..$branch --first-parent | cat -n) | sort -k2 -s | uniq -f1 -d | sort -n | tail -1 | cut -f2'"
show-merge = "!sh -c 'merge=$(git find-merge $0 $1) && [ -n \"$merge\" ] && git show $merge'"
然后你可以像这样使用别名:
# current branch
git find-merge <SHA-1>
# specify master
git find-merge <SHA-1> master
要查看合并提交的消息和其他细节,请使用带有相同参数的git show-merge。
(基于Gauthier的回答。感谢Rosen Matev和javabrett用sort纠正了一个问题。)
其他回答
我需要这样做,并以某种方式找到了git-when-merged(它实际上引用了这个SO问题,但Michael Haggerty从未在这里添加对他非常出色的Python脚本的引用)。现在我有了。
我使用下面的bash脚本,我放置在路径~/bin/git-find-merge。它基于Gauthier的答案和evilstreak的答案,并在处理极端情况时做了一些调整。当输入未排序时,Comm抛出。Grep -f工作得很完美。
角情况下:
如果commit是分支的第一个父路径上的合并提交,则返回commit。 如果commit在分支的第一个父路径上是非合并提交,则返回分支。要么是ff merge,要么是只在分支上提交没有办法确定正确的提交。 如果commit和branch相同,则返回commit。
~ / bin / git-find-merge脚本:
#!/bin/bash
commit=$1
if [ -z $commit ]; then
echo 1>&2 "fatal: commit is required"
exit 1
fi
commit=$(git rev-parse $commit)
branch=${2-@}
# if branch points to commit (both are same), then return commit
if [ $commit == $(git rev-parse $branch) ]; then
git log -1 $commit
exit
fi
# if commit is a merge commit on first-parent path of branch,
# then return commit
# if commit is a NON-merge commit on first-parent path of branch,
# then return branch as it's either a ff merge or commit is only on branch
# and there is not a good way to figure out the right commit
if [[ $(git log --first-parent --pretty='%P' $commit..$branch | \
cut -d' ' -f1 | \
grep $commit | wc -l) -eq 1 ]]; then
if [ $(git show -s --format="%P" $commit | wc -w) -gt 1 ]; then
# if commit is a merge commit
git log -1 $commit
else
# if commit is a NON-merge commit
echo 1>&2 ""
echo 1>&2 "error: returning the branch commit (ff merge or commit only on branch)"
echo 1>&2 ""
git log -1 $branch
fi
exit
fi
# 1st common commit from bottom of first-parent and ancestry-path
merge=$(grep -f \
<(git rev-list --first-parent $commit..$branch) \
<(git rev-list --ancestry-path $commit..$branch) \
| tail -1)
if [ ! -z $merge ]; then
git log -1 $merge
exit
fi
# merge commit not found
echo 1>&2 "fatal: no merge commit found"
exit 1
这让我可以这样做:
(master)
$ git find-merge <commit> # to find when commit merged to current branch
$ git find-merge <branch> # to find when branch merged to current branch
$ git find-merge <commit> pu # to find when commit merged to pu branch
这个脚本也可以在我的github。
您的示例显示分支特性仍然可用。
在这种情况下,h是最后的结果:
git log master ^feature --ancestry-path
如果分支特性不再可用,你可以在c和master之间的历史行中显示合并提交:
git log <SHA-1_for_c>..master --ancestry-path --merges
然而,这也将显示所有发生在h之后的合并,以及在e和g之间的特征。
比较以下命令的结果:
git rev-list <SHA-1_for_c>..master --ancestry-path
git rev-list <SHA-1_for_c>..master --first-parent
会得到h的SHA-1作为最后一行。
如果有可用的,可以在这些结果上使用comm -1 -2。如果你在msysgit上,你可以使用下面的perl代码进行比较:
perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' file1 file2
(perl代码来自http://www.cyberciti.biz/faq/command-to-display-lines-common-in-files/,这是从“comp.com .unix.shell新闻组的某人”)。
如果要使其成为一行程序,请参阅流程替换。
我的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
对于Ruby人群来说,有git- where。非常容易。
$ gem install git-whence
$ git whence 1234567
234557 Merge pull request #203 from branch/pathway