代码之家  ›  专栏  ›  技术社区  ›  Kornel

如何为git重基选择合并策略?

  •  124
  • Kornel  · 技术社区  · 14 年前

    git-rebase -X<option> 可以传递给 git-merge . 具体时间/方式?

    我想通过使用 递归的 战略和 他们的

    我试过:

    git rebase -Xtheirs
    

    git rebase -s 'recursive -Xtheirs'
    

    但git拒绝了 -X 在这两种情况下。


    git rebase -Xtheirs 适用于最新版本,但树冲突需要手动解决。你得跑了 git rebase -Xtheirs --continue (与

    3 回复  |  直到 9 年前
        1
  •  251
  •   Elijah Lynn    5 年前

    您可以在gitv1.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} 正如 documentation )

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

    “我们的”和“他们的”的意思与他们在直接合并时所做的相反。 换言之,“他们的”更倾向于 分支机构。

        2
  •  20
  •   abatishchev Karl Johan    8 年前

    这是为合并策略提供的,合并策略有自己的一组选项

    git rebase <branch> -s recursive -X theirs
    

    不过,应该管用 this patch mentions (2010年2月):

    手册上说 git-rebase 支持合并策略,但 司令部不知道 -X ,并给出了它的用法。

    因此,如果它仍然不起作用,它现在正在辩论中!


    更新自 commit db2b3b820e2b28da268cc88adff076b396392dfe

    合并策略及其选项可以在中指定 git rebase ,但与 -- interactive

    签字人:Arnaud Fontaine

    -十 现在,这个策略可以与交互式重新基以及普通重新基一起工作。

        3
  •  7
  •   Community CDub    7 年前

    作为 iCrazy

    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