开发者

How can I see what branch another branch was forked from?

My git repository has three branches, devel, stable and customers/acme_patches. A long time ago, stable was forked from devel, and all the bugfixing takes place in stable. Every now and then, stable is merged back into devel. customers/acme_patches is a branch with a few customer-specific patches. The branch wasn't merged into either of devel and stable.

A bit of ASCII art to illustrate the scenario:

            o---o---o          customers/acme_patches?
           /
  o---o---1---o---o---o        stable
 /     \           \
o---o---o---2---o---o---o---o  devel
             \
              o---o---o        customers/acme_patches?

Now I wonder:

What branch was customers/acme_patches forked from - devel or stable? I only know that it was forked off one of them in the past, but I don't know which. E.g. it might have been commit 1 or 2 in the above diagram.

I've been playing around with git log --oneline --graph and gitk but since customers/acme_patches was forked a few hundred commits ago, it's hard to follow t开发者_开发问答he lines being drawn.

Is there maybe a quick command (a little script is fine, too) which can somehow follow the commits in customers/acme_patches backwards to find the first commit with two children (the fork point) and then determines whether that commit was done in stable or in devel?

In the best case, I could just execute something like (excuse the prompt, I'm on Windows):

C:\src> git fork-origin customers/acme_patches
stable


With git 1.9/2.0 (Q1 2014), you can use git merge-base --fork-point to ask for the best common ancestor according to Git.

You can see that new option:

  • detailed in "How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?".
  • used in "How do you deal with a public repository that has already been rebased?".

And since commit ad8261d from John Keeping (johnkeeping), git rebase can use that same new --fork-point option, which can come in handy should you need to rebase a branch like customers/acme_patches onto devel.
(I am not saying this would make sense in your specific scenario)


Note: Git 2.16 (Q1 2018) does clarify and enhance documentation for "merge-base --fork-point", as it was clear what it computed but not why/what for.

See commit 6d1700b (09 Nov 2017) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 022dd4a, 27 Nov 2017)

merge-base --fork-point doc: clarify the example and failure modes

The illustrated history used to explain the --fork-point mode named three keypoint commits B3, B2 and B1 from the oldest to the newest, which was hard to read.
Relabel them to B0, B1, B2.
Also illustrate the history after the rebase using the --fork-point facility was made.

The text already mentions use of reflog, but the description is not clear what benefit we are trying to gain by using reflog.
Clarify that it is to find the commits that were known to be at the tip of the remote-tracking branch.
This in turn necessitates users to know the ramifications of the underlying assumptions, namely, expiry of reflog entries will make it impossible to determine which commits were at the tip of the remote-tracking branches and we fail when in doubt (instead of giving a random and incorrect result without even warning).
Another limitation is that it won't be useful if you did not fork from the tip of a remote-tracking branch but from in the middle.
Describe them.

So the documentation now reads:

After working on the topic branch created with git checkout -b topic origin/master, the history of remote-tracking branch origin/master may have been rewound and rebuilt, leading to a history of this shape:

                 o---B2
                /
---o---o---B1--o---o---o---B (origin/master)
        \
         B0
          \
           D0---D1---D (topic)

where origin/master used to point at commits B0, B1, B2 and now it points at B, and your topic branch was started on top of it back when origin/master was at B0, and you built three commits, D0, D1, and D, on top of it.
Imagine that you now want to rebase the work you did on the topic on top of the updated origin/master.

In such a case, git merge-base origin/master topic would return the parent of B0 in the above picture, but B0^..D is not the range of commits you would want to replay on top of B (it includes B0, which is not what you wrote; it is a commit the other side discarded when it moved its tip from B0 to B1).

git merge-base --fork-point origin/master topic is designed to help in such a case.
It takes not only B but also B0, B1, and B2 (i.e. old tips of the remote-tracking branches your repository's reflog knows about) into account to see on which commit your topic branch was built and finds B0, allowing you to replay only the commits on your topic, excluding the commits the other side later discarded.

Hence

$ fork_point=$(git merge-base --fork-point origin/master topic)

will find B0, and

$ git rebase --onto origin/master $fork_point topic

will replay D0, D1 and D on top of B to create a new history of this shape:

         o---B2
        /
---o---o---B1--o---o---o---B (origin/master)
    \                   \
     B0                  D0'--D1'--D' (topic - updated)
      \
       D0---D1---D (topic - old)

A caveat is that older reflog entries in your repository may be expired by git gc.
If B0 no longer appears in the reflog of the remote-tracking branch origin/master, the --fork-point mode obviously cannot find it and fails, avoiding to give a random and useless result (such as the parent of B0, like the same command without the --fork-point option gives).

Also, the remote-tracking branch you use the --fork-point mode with must be the one your topic forked from its tip.
If you forked from an older commit than the tip, this mode would not find the fork point (imagine in the above sample history B0 did not exist, origin/master started at B1, moved to B2 and then B, and you forked your topic at origin/master^ when origin/master was B1; the shape of the history would be the same as above, without B0, and the parent of B1 is what git merge-base origin/master topic correctly finds, but the --fork-point mode will not, because it is not one of the commits that used to be at the tip of origin/master).


Well, there is probably no perfect solution to this answer. I mean there is no fork-origin equivalent in git (to my knowledge). Because the stable branch is merged into devel, your acme_patches (from 1) is on both devel and stable branch.

What you could possibly do is:

git branch --contains $(git merge-base customers/acme_patches devel stable)

If you have stable and not devel, or devel and not stable, then you know where it comes from.

For example, in the case 2, you would have

$ git branch --contains $(git merge-base customers/acme_patches devel stable)
customers/acme_patches
devel

while in case 1 you would have

$ git branch --contains $(git merge-base customers/acme_patches devel stable)
customers/acme_patches
devel
stable

As it's now on both branches (because of the merge from stable to dev)


well, git merge-base customers/acme_patches stable should show the common ancestor of those two branches.

You could try, for instance, gitk --left-right customers/acme_patches...stable (note three dots!). This will show all the commits that are in those branches and not in the merge base. Using --left-right will mark each commit with a left or right arrow according to which branch they are in- a left arrow if they are in customers/acme_patches and a right arrow if they are in stable.

Possibly also add --date-order which I've found sometimes helps make sense of the output.

(You can use this syntax with git log --graph rather than gitk but imho this is a case where the visual graph display is a big improvement).


Not sure if it covers all cases, but here's the functions that I came up with:

git_branch_contains() {
    local b=$1
    local c=$2
    IFS_=$IFS
    IFS=$'\n'
    local branches=($(git branch --contains "$c" | sed -E 's/^(\*| ) //'))
    IFS=$IFS_
    for b2 in "${branches[@]:+${branches[@]}}"; do
        if [ "$b2" = "$b" ]; then
            return 0
        fi
    done
    return 1
}

git_upstream_branch() {
    local b=$1
    local c1=$(git merge-base --fork-point master "$b") || local c1=
    local c2=$(git merge-base --fork-point dev "$b") || local c2=
    if ! [ "$c1" ]; then
        echo dev
        return
    fi
    if ! [ "$c2" ]; then
        echo master
        return
    fi
    local fp
    if git merge-base --is-ancestor "$c1" "$c2"; then
        fp=$c2
    else
        fp=$c1
    fi
    if git_branch_contains master "$fp" && ! git_branch_contains dev "$fp"; then
        echo master
    else
        echo dev
    fi
}

And here's the script to test them (git-upstream-branch-test.sh):

#!/usr/bin/env bash
set -eu

. git-upstream-branch.sh

git_commit() {
    if ! [ "${commit_i:-}" ]; then
        commit_i=0
    fi
    (( commit_i++ )) || true
    echo "$commit_i" > "$commit_i"
    git add "$commit_i"
    git commit -qm "c$commit_i"
}

git_merge() {
    if ! [ "${merge_i:-}" ]; then
        merge_i=0
    fi
    (( merge_i++ )) || true
    git merge -m "$merge_i" $1
}

A_TOPOLOGY=${1:-}

mkdir git-upstream-branch-test-repo
cd git-upstream-branch-test-repo
git init -q
if [ "$A_TOPOLOGY" = 10 ]; then
    git_commit
    git_commit
    git checkout -qb dev
    git_commit
    git_commit
    git checkout -q master
    git_commit
    git_commit
    c=$(git rev-parse HEAD)
    git_commit
    git_commit
    git checkout -q dev
    git checkout -qb t1
    git_commit
    git_commit
    git checkout -q dev
    git_commit
    git_commit
    git rebase --onto "$c" dev t1
elif [ "$A_TOPOLOGY" = 11 ]; then
    git_commit
    git_commit
    git checkout -qb dev
    git_commit
    git_commit
    git checkout -q master
    git_commit
    git_commit
    git checkout -q dev
    c=$(git rev-parse HEAD)
    git_commit
    git_commit
    git checkout -q master
    git checkout -qb t1
    git_commit
    git_commit
    git checkout -q master
    git_commit
    git_commit
    git rebase --onto "$c" master t1
else
    git_commit
    git_commit
    git checkout -qb dev
    git_commit
    git_commit
    git checkout -q master
    git_commit
    git_commit
    if [ "$A_TOPOLOGY" = 4 ] || [ "$A_TOPOLOGY" = 5 ] || [ "$A_TOPOLOGY" = 6 ]; then
        git_merge dev
        git_commit
        git_commit
        git checkout -q dev
        git_commit
        git_commit
        git checkout -q master
    elif [ "$A_TOPOLOGY" = 7 ] || [ "$A_TOPOLOGY" = 8 ] || [ "$A_TOPOLOGY" = 9 ]; then
        git checkout -q dev
        git_merge master
        git_commit
        git_commit
        git checkout -q master
        git_commit
        git_commit
    fi
    git checkout -qb t1
    git_commit
    git_commit
    git checkout -q master
    git_commit
    git_commit
    if [ "$A_TOPOLOGY" = 2 ] || [ "$A_TOPOLOGY" = 5 ] || [ "$A_TOPOLOGY" = 8 ]; then
        git_merge dev
    elif [ "$A_TOPOLOGY" = 3 ] || [ "$A_TOPOLOGY" = 6 ] || [ "$A_TOPOLOGY" = 9 ]; then
        git checkout -q dev
        git_merge master
    fi
fi
git --no-pager log --oneline --graph --decorate --all
git_upstream_branch t1

Use it like so,

$ rm -rf git-upstream-branch-test-repo && ./git-upstream-branch-test.sh NUMBER

Where NUMBER is a number from 1 to 11 to specify which case (topology) to test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜