如何为Git Rebase选择合并策略?

185

git-rebase的手册提到可以通过-X<option>选项传递给git-merge。何时/如何使用?

我想通过应用补丁来进行变基,使用递归策略和theirs选项(应用任何符合要求的内容,而不是跳过整个有冲突的提交)。我不想合并,我想让历史线性化。

我尝试过:

git rebase -Xtheirs

并且

git rebase -s 'recursive -Xtheirs'

但是git在这两种情况下都会拒绝使用-X


git rebase -Xtheirs 在最新版本中可行,但是树冲突需要手动解决。您需要在解决这些冲突后运行git rebase -Xtheirs --continue(重复使用-X)。


请注意:现在这也适用于 git rebase --interactive。请查看我下面的更新答案 - VonC
3个回答

305

你可以在Git v1.7.3或更高版本中使用此功能。

git rebase --strategy-option theirs ${branch} # Long option
git rebase -X theirs ${branch} # Short option

(这是一个缩写,表示为git rebase --strategy recursive --strategy-option theirs ${branch},根据文档所述。)

来自Git v1.7.3版本的发行说明:

git rebase --strategy <s>学会了--strategy-option/-X选项,以传递由所选择合并策略理解的额外选项。

NB:"Ours" 和 "theirs" 的含义与直接合并时相反。 换句话说,“theirs”更偏向于在当前分支上提交的提交。


7
澄清一下:$ git rebase --strategy recursive -X theirs该命令用于 Git 中的代码重组,使用递归策略并选用 "theirs" 选项。 - Gregg Lind
44
当我尝试这样做时,ourstheirs的含义似乎与我的预期相反。我需要使用theirs来支持我的当前分支。 - Craig McQueen
26
当使用rebase时,你未发布(未推送)的提交将被搁置,分支将与远程对齐(快进),然后你的提交将被重新应用到你的分支之上。 根据合并操作,你的提交属于“他们”,本地分支当前的(快进的)状态是“我们”的。 这可能看起来违反直觉,但一旦你意识到实际发生了什么,就会变得有意义。 - patrikbeno
8
引用奥比万·克诺比的话,“所以我告诉你的是真的...从某种程度上来说。” - Craig McQueen
5
我不确定是否值得添加,但至少在相对较新的版本中,存在“-X”意味着“-s recursive”,因此现在可以只使用“git rebase ${branch} -X theirs”。(来源https://git-scm.com/docs/git-rebase#git-rebase--Xltstrategy-optiongt) - Matt Passell
显示剩余5条评论

24

这是针对带有自己选项集的合并策略的。

git rebase <branch> -s recursive -X theirs

虽然 这个补丁 (2010年2月)提到:“虽然手册说git rebase支持合并策略,但是rebase命令不知道-X,当它被使用时会给出用法。” 但是应该可以工作。

所以如果还不起作用,就正在进行讨论!
(在最近的git中得到支持)


来自 提交 db2b3b820e2b28da268cc88adff076b396392dfe (2013年7月,git 1.8.4+)的更新:

在交互式rebase中不要忽略合并选项

合并策略及其选项可以在git rebase中指定,但是使用--interactive时,它们将被完全忽略。

Signed-off-by: Arnaud Fontaine

这意味着-X和策略现在可以与交互式rebase以及普通rebase一起使用。


1
@porneL: 我也是这么想的。所以我才提供了修补方案的链接。 - VonC
@porneL:是的,我也注意到了这个bug - 我预计它很快就会得到解决,无论是通过那个补丁还是其他方式,因为所有基本设施都在那里;他们只需要决定从rebase到merge的通信方式。 - Cascabel
@porneL:它已经包含在git 1.7.3中。如果你仍然像我一样是1.7.1用户,那么有一个简单的解决方案,请查看我的下面的答案。 - MestreLion

7
正如iCrazy所说,这个功能只适用于git 1.7.3及以上版本。因此,对于那些(像我一样)仍在使用1.7.1的可怜人,我自己提出了一个解决方案: git-rebase-theirs 这是一个非常精细(因此很长)的脚本,旨在用于生产:ui选项,处理多个文件,检查文件是否实际上有冲突标记等,但“核心”可以概括为两行:
cp file file.bak
awk '/^<+ HEAD$/,/^=+$/{next} /^>+ /{next} 1' file.bak > file

这里是完整的脚本:

#!/bin/bash
#
# git-rebase-theirs - Resolve rebase conflicts by favoring 'theirs' version
#
#    Copyright (C) 2012 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program. If not see <http://www.gnu.org/licenses/gpl.html>

#Defaults:
verbose=0
backup=1
inplace=0
ext=".bak"

message() { printf "%s\n" "$1" >&2 ; }
skip()    { message "skipping ${2:-$file}${1:+: $1}"; continue ; }
argerr()  { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
invalid() { argerr "invalid option: $1" ; }
missing() { argerr "missing${1:+ $1} operand." ; }

usage() {
    cat <<- USAGE
    Usage: $myname [options] [--] FILE...
    USAGE
    if [[ "$1" ]] ; then
        cat >&2 <<- USAGE
        Try '$myname --help' for more information.
        USAGE
        exit 1
    fi
    cat <<-USAGE

    Resolve git rebase conflicts in FILE(s) by favoring 'theirs' version

    When using git rebase, conflicts are usually wanted to be resolved
    by favoring the <working branch> version (the branch being rebased,
    'theirs' side in a rebase), instead of the <upstream> version (the
    base branch, 'ours' side)

    But git rebase --strategy -X theirs is only available from git 1.7.3
    For older versions, $myname is the solution.

    It works by discarding all lines between '<<<<<<< HEAD' and '========'
    inclusive, and also the the '>>>>>> commit' marker.

    By default it outputs to stdout, but files can be edited in-place
    using --in-place, which, unlike sed, creates a backup by default.

    Options:
      -h|--help            show this page.
      -v|--verbose         print more details in stderr.

      --in-place[=SUFFIX]  edit files in place, creating a backup with
                           SUFFIX extension. Default if blank is ""$ext"

       --no-backup         disables backup

    Copyright (C) 2012 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
    License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
    USAGE
    exit 0
}
myname="${0##*/}"

# Option handling
files=()
while (( $# )); do
    case "$1" in
    -h|--help     ) usage            ;;
    -v|--verbose  ) verbose=1        ;;
    --no-backup   ) backup=0         ;;
    --in-place    ) inplace=1        ;;
    --in-place=*  ) inplace=1
                    suffix="${1#*=}" ;;
    -*            ) invalid "$1"     ;;
    --            ) shift ; break    ;;
    *             ) files+=( "$1" )  ;;
    esac
    shift
done
files+=( "$@" )

(( "${#files[@]}" )) || missing "FILE"

ext=${suffix:-$ext}

for file in "${files[@]}"; do

    [[ -f "$file" ]] || skip "not a valid file"

    if ((inplace)); then
        outfile=$(tempfile) || skip "could not create temporary file"
        trap 'rm -f -- "$outfile"' EXIT
        cp "$file" "$outfile" || skip
        exec 3>"$outfile"
    else
        exec 3>&1
    fi

    # Do the magic :)
    awk '/^<+ HEAD$/,/^=+$/{next} /^>+ /{next} 1' "$file" >&3

    exec 3>&-

    ((inplace)) || continue

    diff "$file" "$outfile" >/dev/null && skip "no conflict markers found"

    ((backup)) && { cp "$file" "$file$ext" || skip "could not backup" ; }

    cp "$outfile" "$file" || skip "could not edit in-place"

    ((verbose)) && message "resolved ${file}"
done

谢谢@VonC!我只是不确定为什么SO没有对bash脚本进行颜色编码。像这样的大型脚本本身总是很丑陋...但是成为一大堆黑色文本使其更加丑陋:P - MestreLion
这个在 http://stackoverflow.com/editing-help#syntax-highlighting 有详细的解释。我已经在你的代码块前添加了适当的 prettify 语言代码。现在应该看起来更好了。 - VonC
感谢@VonC!SO的语法高亮确实不太好,但比没有要好得多。而且你非常周到!作为SO中THE git权威,您可能会对另一个辅助脚本感兴趣:https://dev59.com/h3A75IYBdhLWcg3wxsJn#10220276。那个和我的github账户有一些你可能会喜欢的工具。 - MestreLion
对于1.7.1版本,这对我来说似乎有效;不需要上面的脚本。 git rebase --strategy="recursive --theirs" master - Papadeltasierra
抱歉我是一个 Git 新手,但是如何使用上面提供的 git-rebase-theirs 脚本呢?它是作为传递给 git-rebase 的选项吗?还是只是减少手动解决冲突所需的时间? - Papadeltasierra
@Papadeltasierra:它只是通过自动查找和替换冲突标记来解决冲突。请注意,此脚本仅适用于1.7.3之前的古老git版本。从1.7.3开始,您可以简单地使用git rebase -s recursive -X theirs <branch>,就像iCrazy回答的那样。 - MestreLion

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