首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何为git rebase选择合并策略?

如何为git rebase选择合并策略?
EN

Stack Overflow用户
提问于 2010-06-01 02:33:46
回答 2查看 97.2K关注 0票数 171

git-rebase手册页提到可以将-X<option>传递给git-merge。确切的时间/方式?

我想通过应用带有递归策略的补丁和他们的选项(应用任何东西,而不是跳过整个冲突的提交)来重新建立基础。我不想合并,我想让历史成为线性的。

我试过了:

代码语言:javascript
复制
git rebase -Xtheirs

代码语言:javascript
复制
git rebase -s 'recursive -Xtheirs'

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

除了树冲突需要手动解决之外,git rebase -Xtheirs在最近的版本中都可以工作。在解决这些冲突之后,您需要运行git rebase -Xtheirs --continue (并重复使用-X )。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-25 11:11:43

您可以在Git v1.7.3或更高版本中使用它。

代码语言:javascript
复制
git rebase --strategy-option theirs ${branch} # Long option
git rebase -X theirs ${branch} # Short option

(这是documentation声明的git rebase --strategy recursive --strategy-option theirs ${branch}的缩写)

来自Git v1.7.3发行说明:

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

注意:“我们的”和“他们的”的意思与他们在直接合并时所做的相反。换句话说,“他们的”偏爱当前分支上的提交。”的意思是“他们的”偏爱当前分支上的提交。

票数 264
EN

Stack Overflow用户

发布于 2012-04-18 06:57:55

正如iCrazy所说,此功能仅适用于git 1.7.3及以上版本。因此,对于那些仍在使用1.7.1的可怜的人(像我),我提出了一个我自己做的解决方案:

git-rebase-theirs

这是一个非常精致(因此很长)的脚本,用于生产用途: ui选项,处理多个文件,检查文件是否真的有冲突标记,等等,但“核心”可以总结为两行:

代码语言:javascript
复制
cp file file.bak
awk '/^<+ HEAD$/,/^=+$/{next} /^>+ /{next} 1' file.bak > file

下面是完整的脚本:

代码语言:javascript
复制
#!/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
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2945344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档