重定基一个分支并包含所有子分支

100

我有以下的Git仓库拓扑结构:

A-B-F (master)
   \   D (feature-a)
    \ /
     C (feature)
      \
       E (feature-b)

我期望通过将feature分支变基来重新变基整个子树(包括子分支):

$ git rebase feature master

A-B-F (master)
     \   D (feature-a)
      \ /
       C (feature)
        \
         E (feature-b)

然而,这是实际的结果:

      C' (feature)
     /
A-B-F (master)
   \   D (feature-a)
    \ /
     C
      \
       E (feature-b)

我知道我可以通过手动执行以下操作轻松修复它:

$ git rebase --onto feature C feature-a
$ git rebase --onto feature C feature-b

但是是否有一种自动将分支及其所有子分支/后代进行变基的方法呢?


3
请参考 如何对一个包含多个分支及其之间由合并操作产生的链接的子历史记录进行变基。不过,这个解决方案中比较麻烦的一点是需要在变基后重新设置主题分支引用指向新的提交记录。 - imz -- Ivan Zakharyaschev
感谢您提到 git rebase 的 --onto 选项 - 它解决了我的问题。 - jackocnr
7
“$ git rebase feature master” 不应该变成 “$ git rebase master feature” 吗? - hbogert
可能是 Git rebase subtree 的重复问题。 - carnicer
5个回答

45
git branch --format='%(refname:short)' --contains C | \
xargs -n 1 \
git rebase --committer-date-is-author-date --onto F C^

3
Rebase onto需要最早提交的父提交作为起点 - 因此是C^。 - Adam Dymitruk
3
如果要重新定位其中一个分支时当前分支已被选中,"git branch" 命令会在当前分支前输出一个星号,这会破坏这个脚本。 - Mark Lodato
2
Git分支不是一个外壳命令吗?有没有一种更具未来性的方法来做这件事? - Chris Pfohl
7
Adam: 不确定那是正确的方法,你需要保留带有的行,只是不要本身。使用类似 | tr -d * 这样的命令可能更合适。我想问的是:为什么要使用 --onto B?我以为应该在 master 上重新打基础。而且 C^ 不等于 B,所以我们是从B(排除?)到每个包含C的分支上重新打基础。结果难道不会与以前完全相同吗? - Marenz
5
这句话的意思是:“这不应该是 --onto F 而不是 --onto B 吗?因为所有这些提交已经在 B 分支上了,我们现在要将它们移动到 F 分支上。” - Ad N
显示剩余5条评论

11
几年前我写了一些东西来处理这种情况。(当然欢迎改进意见,但不要太苛刻——那是很久以前的事情!我甚至还不知道Perl!)
它适用于更静态的情况——通过设置形式为branch.<branch>.autorebaseparent的配置参数进行配置。它不会触及任何没有设置该配置参数的分支。如果这不是你想要的,你可能可以轻松地将它修改为你想要的样子。在过去的一两年中,我并没有真正经常使用它,但当我使用它时,它似乎总是非常安全和稳定,就像大规模自动重置那样。
所以这就是它的用法。把它保存到一个名为git-auto-rebase的文件中,放在你的PATH中使用。在实际操作之前最好使用干运行(-n)选项。它可能比你想要的更详细,但它会显示它将尝试重新设置的内容以及它将重置到哪里。这可能会节省你一些麻烦。
#!/bin/bash

CACHE_DIR=.git/auto-rebase
TODO=$CACHE_DIR/todo
TODO_BACKUP=$CACHE_DIR/todo.backup
COMPLETED=$CACHE_DIR/completed
ORIGINAL_BRANCH=$CACHE_DIR/original_branch
REF_NAMESPACE=refs/pre-auto-rebase

print_help() {
    echo "Usage:  git auto-rebase [opts]"
    echo "Options:"
    echo "    -n   dry run"
    echo "    -c   continue previous auto-rebase"
    echo "    -a   abort previous auto-rebase"
    echo "         (leaves completed rebases intact)"
}

cleanup_autorebase() {
    rm -rf $CACHE_DIR
    if [ -n "$dry_run" ]; then
        # The dry run should do nothing here. It doesn't create refs, and won't
        # run unless auto-rebase is empty. Leave this here to catch programming
        # errors, and for possible future -f option.
        git for-each-ref --format="%(refname)" $REF_NAMESPACE |
        while read ref; do
            echo git update-ref -d $ref
        done
    else
        git for-each-ref --format="%(refname)" $REF_NAMESPACE |
        while read ref; do
            git update-ref -d $ref
        done
    fi
}

# Get the rebase relationships from branch.*.autorebaseparent
get_config_relationships() {
    mkdir -p .git/auto-rebase
    # We cannot simply read the indicated parents and blindly follow their
    # instructions; they must form a directed acyclic graph (like git!) which
    # furthermore has no sources with two sinks (i.e. a branch may not be
    # rebased onto two others).
    # 
    # The awk script checks for cycles and double-parents, then sorts first by
    # depth of hierarchy (how many parents it takes to get to a top-level
    # parent), then by parent name. This means that all rebasing onto a given
    # parent happens in a row - convenient for removal of cached refs.
    IFS=$'\n'
    git config --get-regexp 'branch\..+\.autorebaseparent' | \
    awk '{
        child=$1
        sub("^branch[.]","",child)
        sub("[.]autorebaseparent$","",child)
        if (parent[child] != 0) {
            print "Error: branch "child" has more than one parent specified."
            error=1
            exit 1
        }
        parent[child]=$2
    }
    END {
        if ( error != 0 )
            exit error
        # check for cycles
        for (child in parent) {
            delete cache
            depth=0
            cache[child]=1
            cur=child
            while ( parent[cur] != 0 ) {
                depth++
                cur=parent[cur]
                if ( cache[cur] != 0 ) {
                    print "Error: cycle in branch."child".autorebaseparent hierarchy detected"
                    exit 1
                } else {
                    cache[cur]=1
                }
            }
            depths[child]=depth" "parent[child]" "child
        }
        n=asort(depths, children)
        for (i=1; i<=n; i++) {
            sub(".* ","",children[i])
        }
        for (i=1; i<=n; i++) {
            if (parent[children[i]] != 0)
                print parent[children[i]],children[i]
        }
    }' > $TODO

    # Check for any errors. If the awk script's good, this should really check
    # exit codes.
    if grep -q '^Error:' $TODO; then
        cat $TODO
        rm -rf $CACHE_DIR
        exit 1
    fi

    cp $TODO $TODO_BACKUP
}

# Get relationships from config, or if continuing, verify validity of cache
get_relationships() {
    if [ -n "$continue" ]; then
        if [ ! -d $CACHE_DIR ]; then
            echo "Error: You requested to continue a previous auto-rebase, but"
            echo "$CACHE_DIR does not exist."
            exit 1
        fi
        if [ -f $TODO -a -f $TODO_BACKUP -a -f $ORIGINAL_BRANCH ]; then
            if ! cat $COMPLETED $TODO | diff - $TODO_BACKUP; then
                echo "Error: You requested to continue a previous auto-rebase, but the cache appears"
                echo "to be invalid (completed rebases + todo rebases != planned rebases)."
                echo "You may attempt to manually continue from what is stored in $CACHE_DIR"
                echo "or remove it with \"git auto-rebase -a\""
                exit 1
            fi
        else
            echo "Error: You requested to continue a previous auto-rebase, but some cached files"
            echo "are missing."
            echo "You may attempt to manually continue from what is stored in $CACHE_DIR"
            echo "or remove it with \"git auto-rebase -a\""
            exit 1
        fi
    elif [ -d $CACHE_DIR ]; then
        echo "A previous auto-rebase appears to have been left unfinished."
        echo "Either continue it with \"git auto-rebase -c\" or remove the cache with"
        echo "\"git auto-rebase -a\""
        exit 1
    else
        get_config_relationships
    fi
}

# Verify that desired branches exist, and pre-refs do not.
check_ref_existence() {
    local parent child
    for pair in "${pairs[@]}"; do
        parent="${pair% *}"
        if ! git show-ref -q --verify "refs/heads/$parent" > /dev/null ; then
            if ! git show-ref -q --verify "refs/remotes/$parent" > /dev/null; then
                child="${pair#* }"
                echo "Error: specified parent branch $parent of branch $child does not exist"
                exit 1
            fi
        fi
        if [ -z "$continue" ]; then
            if git show-ref -q --verify "$REF_NAMESPACE/$parent" > /dev/null; then
                echo "Error: ref $REF_NAMESPACE/$parent already exists"
                echo "Most likely a previous git-auto-rebase did not complete; if you have fixed all"
                echo "necessary rebases, you may try again after removing it with:"
                echo
                echo "git update-ref -d $REF_NAMESPACE/$parent"
                echo
                exit 1
            fi
        else
            if ! git show-ref -q --verify "$REF_NAMESPACE/$parent" > /dev/null; then
                echo "Error: You requested to continue a previous auto-rebase, but the required"
                echo "cached ref $REF_NAMESPACE/$parent is missing."
                echo "You may attempt to manually continue from the contents of $CACHE_DIR"
                echo "and whatever refs in refs/$REF_NAMESPACE still exist, or abort the previous"
                echo "auto-rebase with \"git auto-rebase -a\""
                exit 1
            fi
        fi
    done
}

# Create the pre-refs, storing original position of rebased parents
create_pre_refs() {
    local parent prev_parent
    for pair in "${pairs[@]}"; do
        parent="${pair% *}"
        if [ "$prev_parent" != "$parent" ]; then
            if [ -n "$dry_run" ]; then
                echo git update-ref "$REF_NAMESPACE/$parent" "$parent" \"\"
            else
                if ! git update-ref "$REF_NAMESPACE/$parent" "$parent" ""; then
                    echo "Error: cannot create ref $REF_NAMESPACE/$parent"
                    exit 1
                fi
            fi
        fi

        prev_parent="$parent"
    done
}

# Perform the rebases, updating todo/completed as we go
perform_rebases() {
    local prev_parent parent child
    for pair in "${pairs[@]}"; do
        parent="${pair% *}"
        child="${pair#* }"

        # We do this *before* rebasing, assuming most likely any failures will be
        # fixed with rebase --continue, and therefore should not be attempted again
        head -n 1 $TODO >> $COMPLETED
        sed -i '1d' $TODO

        if [ -n "$dry_run" ]; then
            echo git rebase --onto "$parent" "$REF_NAMESPACE/$parent" "$child"
            echo "Successfully rebased $child onto $parent"
        else
            echo git rebase --onto "$parent" "$REF_NAMESPACE/$parent" "$child"
            if ( git merge-ff -q "$child" "$parent" 2> /dev/null && echo "Fast-forwarded $child to $parent." ) || \
                git rebase --onto "$parent" "$REF_NAMESPACE/$parent" "$child"; then
                echo "Successfully rebased $child onto $parent"
            else
                echo "Error rebasing $child onto $parent."
                echo 'You should either fix it (end with git rebase --continue) or abort it, then use'
                echo '"git auto-rebase -c" to continue. You may also use "git auto-rebase -a" to'
                echo 'abort the auto-rebase. Note that this will not undo already-completed rebases.'
                exit 1
            fi
        fi

        prev_parent="$parent"
    done
}

rebase_all_intelligent() {
    if ! git rev-parse --show-git-dir &> /dev/null; then
        echo "Error: git-auto-rebase must be run from inside a git repository"
        exit 1
    fi

    SUBDIRECTORY_OK=1
    . "$(git --exec-path | sed 's/:/\n/' | grep -m 1 git-core)"/git-sh-setup
    cd_to_toplevel


    # Figure out what we need to do (continue, or read from config)
    get_relationships

    # Read the resulting todo list
    OLDIFS="$IFS"
    IFS=$'\n'
    pairs=($(cat $TODO))
    IFS="$OLDIFS"

    # Store the original branch
    if [ -z "$continue" ]; then
        git symbolic-ref HEAD | sed 's@refs/heads/@@' > $ORIGINAL_BRANCH
    fi

    check_ref_existence
    # These three depend on the pairs array
    if [ -z "$continue" ]; then
        create_pre_refs
    fi
    perform_rebases

    echo "Returning to original branch"
    if [ -n "$dry_run" ]; then
        echo git checkout $(cat $ORIGINAL_BRANCH)
    else
        git checkout $(cat $ORIGINAL_BRANCH) > /dev/null
    fi

    if diff -q $COMPLETED $TODO_BACKUP ; then
        if [ "$(wc -l $TODO | cut -d" " -f1)" -eq 0 ]; then
            cleanup_autorebase
            echo "Auto-rebase complete"
        else
            echo "Error: todo-rebases not empty, but completed and planned rebases match."
            echo "This should not be possible, unless you hand-edited a cached file."
            echo "Examine $TODO, $TODO_BACKUP, and $COMPLETED to determine what went wrong."
            exit 1
        fi
    else
        echo "Error: completed rebases don't match planned rebases."
        echo "Examine $TODO_BACKUP and $COMPLETED to determine what went wrong."
        exit 1
    fi
}


while getopts "nca" opt; do
    case $opt in
        n ) dry_run=1;;
        c ) continue=1;;
        a ) abort=1;;
        * )
            echo "git-auto-rebase is too dangerous to run with invalid options; exiting"
            print_help
            exit 1
    esac
done
shift $((OPTIND-1))


case $# in
    0 )
        if [ -n "$abort" ]; then
            cleanup_autorebase
        else
            rebase_all_intelligent
        fi
        ;;

    * )
        print_help
        exit 1
        ;;
esac

我发现,自从我最初处理这个问题以来,有一件事情是:有时候答案是你根本不想进行变基!在第一次开始主题分支时选择正确的共同祖先并不尝试在此之后将它们向前移动是有道理的。但这是你和你的工作流之间的事情。


赞同“使用合并代替”。在尝试合并选项之前,我花了几个小时尝试将许多主题和子主题分支重新定位,但实际上合并要容易得多,即使新的主分支与原始主分支有很大的差异。 - davenpcj
3
有一点让我感到害怕,答案中包含了“我连Perl都还不会”的内容——特别是因为这个答案并没有用Perl写出来... :-) - Peter V. Mørch
@PeterV.Mørch,是什么意思? - Pacerier
至少我读到这篇答案时,感觉作者知道他需要为此编写一个脚本,并决定使用Perl编写。然后他尝试编写一些Perl代码,但意外地最终得到了一个可以用bash(+一些嵌入式awk)执行的脚本,仍然认为自己已经编写了一些Perl代码。 - Mikko Rantalainen

5

在 Adam 的答案基础上构建,以解决双方分支上的多个提交问题:

A-B-F (master)
   \
    O   D (feature-a)
     \ /
      C (feature)
       \
        T-E (feature-b)

这里有一个更稳定的方法:

[alias]
    # rebases branch with its sub-branches (one level down)
    # useage: git move <upstream> <branch>
    move = "!mv() { git rebase $1 $2; git branch --format='%(refname:short)' --contains $2@{1} | xargs -n 1 git rebase --onto $2 $2@{1}; }; mv"

这样 git move master feature 将会得到预期结果:

A-B-F (master)
     \
      O`   D` (feature-a)
       \ /
        C` (feature)
         \
          T`-E` (feature-b)

工作原理分解:

  • git rebase $1 $2 导致以下结果:
A-B--------------------F (master)
   \                    \
    O   D (feature-a)    O`
     \ /                  \
      C                    C` (feature)
       \
        T-E (feature-b)

注意feature现在位于C`,而不是C

  • 让我们解析git branch --format='%(refname:short)' --contains $2@{1} 这将返回包含以前的位置为Cfeature的分支列表,并将输出格式化为
feature-a
feature-b

feature的前一个位置来自于reflogs$2@{1},它仅仅意味着“第二个参数(功能分支)的上一次位置”。

  • | xargs -n 1 git rebase --onto $2 $2@{1}这一步骤将上述分支列表通过管道符分成不同的重定基命令,并实际上转换为git rebase --onto feature C feature-a; git rebase --onto feature C feature-b

非常有趣的方法!你能解释一下它是如何工作的吗? - Eugen Labun
你在回答中提供了如此丰富的知识:git别名、带有多个命令的别名、使用“!”在别名中定义shell命令、在git别名中使用shell函数以正确处理位置参数、通过“@{n}”符号访问git reflog等等。我学到了很多,谢谢你,Taras! - Eugen Labun
这种方法真的很好。只是对于错误的抵抗有一些想法: 通过在reflog中使用@{1}搜索引用。我猜如果<分支>已经基于<上游>,这可能会产生一些奇怪的副作用。根据<分支>之前是否被重新设置,这可能会导致错误“fatal: log for '<branch>' only has 1 entries”,或者可能会意外地重新设置包含<分支>在其先前位置的分支。 更安全的方法是在重新设置<分支>之前记录其ID,因此在最坏的情况下,所有分支都将重新设置为它们已经存在的位置。 - Ragas
此外,第一个命令以“;”终止。在错误情况下,这将导致仍然尝试重新定位包含<分支>的所有分支,这可能会导致意外行为。最好在这里使用“&&”以确保如果出现问题,则停止执行。我还没有尝试过这个,所以如果我看错了,请纠正我。 - Ragas

1
使用 git-branchless 工具套件,您可以直接对子树进行变基:
$ git move -b feature -d master

免责声明:本文作者。


0
如果需要更新提交者日期,可以使用GIT_COMMITTER_DATE环境变量(manual)。同时,使用--format选项可以获取不带额外格式的分支名称。
export GIT_COMMITTER_DATE=$( date -Iseconds )
git branch --format='%(refname)' --contains C | xargs -n 1 | git rebase -p --onto master C^
unset GIT_COMMITTER_DATE
# don't forget to unset this variable to avoid effect for the further work

注意:为了保证在重新设置featurefeature-afeature-b时,C'Ca'Cb'提交的校验和相同,必须设置--committer-date-is-author-dateGIT_COMMITTER_DATE


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接