Answers:
您可以在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
选项,以传递所选合并策略可以理解的其他选项。
注意:“我们的”和“他们的”是指他们在直接合并期间所做的相反的事情。换句话说,“他们”赞成当前分支上的提交。
ours
和theirs
似乎是我所期望的相反。我需要使用theirs
来支持我当前的分支。
-X
implies 的存在-s recursive
,因此您现在可以使用just git rebase ${branch} -X theirs
。(源git-scm.com/docs/git-rebase#git-rebase--Xltstrategy-optiongt)
这是针对带有各自选项的合并策略
git rebase <branch> -s recursive -X theirs
联机帮助页上说它
git-rebase
支持合并策略,但是rebase命令不了解-X
,并给出了用法。
因此,如果仍然无法正常运行,则目前正在辩论中!
(最近的git支持)
来自提交db2b3b820e2b28da268cc88adff076b396392dfe的更新(2013年7月,git 1.8.4+),
不要忽略交互式变基中的合并选项
可以在中指定合并策略及其选项
git rebase
,但使用-- interactive
可以完全忽略它们。签字人:Arnaud Fontaine
这意味着-X
,策略现在可以与交互式rebase以及普通rebase一起使用。
正如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) <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
git rebase --strategy="recursive --theirs" master
git rebase --interactive
。请参阅我的[更新后的答案(stackoverflow.com/a/2945367/6309)。