Git:如何找到源自派生分支B的分支A中的所有提交合并回A?

时间:2010-06-02 09:24:00

标签: git

在Git中,给出(1)分支A和(2)在过去的某个时刻从A派生的分支B,然后合并回A,我怎样才能找到A中的所有提交,这些提交源自B ?

目的是确定现在在A中执行的工作变更集,以便更快地追踪问题。

压缩提交显然可以方便地将整个变更集打包在一个提交中以便于参考,但缺点(例如信息丢失和个人归属性)使我们不喜欢这个选项。因此我的问题。

3 个答案:

答案 0 :(得分:5)

假设B完全合并到A,您可以使用:

git cherry -v <merge-commit>^ <topic-branch>

...其中:

  • <merge-commit>^是您合并主题分支的提交的父级。
  • <topic-branch>是您要检查的分支

答案 1 :(得分:1)

一旦将分支合并回来,合并提交就是其存在的标记。假设你没有过多地使用你的合并提交消息,你可以这样做:

#!/bin/bash

die_with_usage() {
    # add a git alias for merged-commits to be able to call it like this
    echo "usage: git merged-commits <branch-merged> <branch-merged-into>" 1>&2
    exit 1
}

if [ $# -ne 2 ]; then
    die_with_usage
fi

# Find the merge commits
merges=($(git log --pretty=%H --grep="Merge branch '$1' into $2"))

if [ ${#merges[@]} -eq 0 ]; then
    echo "error: no such merges found!" 1>&2
    die_with_usage
fi

for merge in ${merges[@]}; do
    # The first parent is the merged-into branch
    bar=$merge^1
    # The second is the merged branch
    foo=$merge^2

    # Find the merge base
    base=$(git merge-base $bar $foo)

    # Show the commits
    git log --pretty=%H $base..$foo
done

我认为打印SHA1会很有帮助,然后让你离开并随意做你喜欢的事情,但当然你可以摆弄最后git log的输出格式。

(我甚至测试了这个!可以保留它;它是一个很酷的小内衬。)

你可以做的另一件事(将来)是采用git.git的合并提交消息,它基本上将合并提交的短日志嵌入到合并提交消息中(这里是an example)。有一天会有一种内置的方式来实现这一目标(它已经合并到下一个但不是主人)但是现在你必须自己动手,或者从下一步做出勇敢的行动和建立。

答案 2 :(得分:1)

查看git来源的contrib/区域中的git-resurrect.sh脚本。

usage: git resurrect [-a] [-r] [-m] [-t] [-n] [-b <newname>] <name>

    -b, --branch ...      save branch as  instead of <name>
    -a, --all             same as -l -r -m -t
    -k, --keep-going      full rev-list scan (instead of first match)
    -l, --reflog          scan reflog for checkouts (enabled by default)
    -r, --reflog-merges   scan for merges recorded in reflog
    -m, --merges          scan for merges into other branches (slow)
    -t, --merge-targets   scan for merges of other branches into 
    -n, --dry-run         don't recreate the branch

git-resurrect attempts to find traces of a branch tip called <name>,
and tries to resurrect it.  Currently, the reflog is searched for checkout
messages, and with `-r' also merge messages.  With `-m' and `-t', the
history of all refs is scanned for "Merge <name> into other" /
"Merge <other> into <name>" (respectively) in commit subjects,
which is rather slow but allows you to resurrect other people's
topic branches.