当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何为gitrebase选择合并策略?》经验,为你挑选了3个好方法。

git-rebase手册页提及-X可以传递给git-merge.什么时候/怎么样?

我想通过应用具有递归策略的补丁和他们的选项(应用任何棒,而不是跳过整个冲突的提交)来重新定义.我不想合并,我想让历史变得线性.

我试过了:

git rebase -Xtheirs

git rebase -s 'recursive -Xtheirs'

-X两种情况下git都拒绝了.


git rebase -Xtheirs适用于最新版本,但需要手动解决树冲突.解决这些冲突后,您需要运行git rebase -Xtheirs --continue(-X重复).



1> 小智..:

您可以将它与Git v1.7.3或更高版本一起使用.

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

从Git v1.7.3发行说明:

git rebase --strategy recursive --strategy-option theirs ${branch}学会了git rebase --strategy 传递所选合并策略所理解的额外选项的选项.

注意:"我们的"和"他们的"意味着他们在直接合并期间所做的相反.换句话说,"他们的"赞成当前分支的提交.

更新:编辑更清晰.


当我尝试这个时,"我们的"和"他们的"的含义似乎与我的期望相反.我需要使用`theirs`来支持我目前的分支.
@CraigMcQueen,当使用rebase时,你的未发布(未发布)提交被放在一边,branch与remote(快进)对齐,你的提交正在你的分支上重放..根据合并操作,您的提交是"他们的",本地分支的当前(快速转发)状态是"我们的".可能看似违反直觉,但一旦你意识到实际发生了什么,这是有道理的.
澄清:$ git rebase --strategy recursive -X他们的
@patrikbeno:引用Obi-Wan Kenobi的话,"所以我从某个角度来看你说的是真的......"
我不确定它是否值得添加,但至少在相对当前版本中,`-X`的存在意味着`-s recursive`,所以你现在可以只使用`git rebase $ {branch} -X theirs`.(来源https://git-scm.com/docs/git-rebase#git-rebase--Xltstrategy-optiongt)

2> VonC..:

这适用于带有各自选项的合并策略

git rebase  -s recursive -X theirs

应该有用,虽然这个补丁提到(2010年2月):

该联机帮助页说git-rebase支持合并策略,但是rebase命令不知道-X,并在提供它时给出了用法.

所以如果它仍然不起作用,它现在正在辩论!
(最近的git支持)


从提交更新db2b3b820e2b28da268cc88adff076b396392dfe(2013年7月,git 1.8.4+),

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

合并策略及其选项可以指定git rebase,但是-- interactive,它们被完全忽略.

签名:Arnaud Fontaine

这意味着-X和策略现在可以使用交互式rebase,以及简单的rebase.



3> MestreLion..:

正如iCrazy所说,此功能仅适用于git 1.7.3及更高版本.所以,对于仍然使用1.7.1的可怜的灵魂(像我一样),我提出了一个我自己做的解决方案:

混帐底垫,他们的

它是一个非常精心打造(因此很长)的脚本,用于生产用途: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) 
#
#    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 

#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  version (the branch being rebased,
    'theirs' side in a rebase), instead of the  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) 
    License: GPLv3 or later. See 
    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

推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有