在许多git主题分支中找到一个旧补丁

时间:2014-08-05 09:13:10

标签: git git-branch

与任何优秀的FLOSS开发人员一样,我正在建立一些本地分支机构,其中包含各种补丁选项。麻烦的是当我处理一个主题时,我可能已经做了一些微不足道的补丁,我打算稍后提取并提交上游。现在我知道我修补过的文件,但我记不起当时的分支。

有没有一种简单的方法可以查询git“哪些分支有路径/到/文件的补丁超过原始/主要的”?

显然,我正试图避免在我的每个主题分支上手动运行“git log $ {BRANCH} - path / to / file”。

3 个答案:

答案 0 :(得分:1)

找到有趣的提交的另一种方法

git log --branches ^origin/master -- $file

(我通常会在这里添加--oneline)。问题是这会获取提交ID,但不会告诉您哪些分支包含这些提交。

添加--decorate,如果有任何提交恰好是分支提示或标记,您将获得显示的名称。但是,假设$ file在commit d0gf00d中被修改,并且链接就是这样的(即,你从`git log --oneline --decorate --branches得到的东西开始于此):< / p>

badcafe (HEAD, master) make it all work
d0gf00d modify $file
affab1e (work) clean up documentation
2fab1e5 initial changes to $file
f1eeced (origin/master) preliminary version
...

此处d0gf00d位于您的分支master上,而不在origin/master上。同时,提交2fab1e5也位于您的分支master上的work 上,而不是origin/master上。其他提交在/ origin/master之上/之下。但是,一旦您过滤掉提交badcafeaffab1e,就不再显示分支标签了。

您可以使用git branch --contains查看(一次一个)哪个分支在其历史记录中有d0gf00daffab1e,但现在您又回到了外壳

答案 1 :(得分:0)

实际上为此编写一个bash函数似乎相当容易。我仍然想知道它是否可以直接使用。

#
# Report git log for a given file across all branches in the system
#
function git_branches_touching
{
    local file=$1
    local branches=()
    eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
    for branch in "${branches[@]}"; do
        echo "Checking $branch"
        git log origin/master..$branch -- $file
    done
}

答案 2 :(得分:0)

作为参考,我制作了一个更漂亮的实用程序脚本(我称之为git branches-touching):

#!/bin/bash
#
# List branches touching
#

hashes="`git log --format=format:"%H" --branches ^origin/master -- ${1}`"
for h in ${hashes}; do
    branches=`git branch --contains $h`
    git log --oneline --format=format:"%C(auto)%h (%Cgreen${branches##  }%Creset, %cr) %s" $h^..$h
done

运行时给出了这个:

12:43 alex@zen/x86_64 [qemu.git/armv8-gdb-kvm@origin] >git branches-touching scripts/qemu-binfmt-conf.sh
04d15fc (armv8-migration, 3 months ago) scripts/qemu-binfmt-conf.sh: re-factor and clean-up
30bc3b4 (ajb-a64-system, 5 months ago) scripts/qemu-binfmt-conf.sh: re-factor and clean-up
406fb35 (ajb-a64-working, 7 months ago) scripts/qemu-binfmt-conf.sh: re-factor and clean-up