如何为 git rebase 选择合并策略?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I select a merge strategy for a git rebase?
提问by Kornel
git-rebase
man page mentions -X<option>
can be passed to git-merge
. When/how exactly?
git-rebase
手册页提及-X<option>
可以传递给git-merge
. 什么时候/具体如何?
I'd like to rebase by applying patches with recursivestrategy and theirsoption (apply whatever sticks, rather than skipping entire conflicting commits). I don't want merge, I want to make history linear.
我想通过应用带有递归策略和他们的选项的补丁来重新定位(应用任何坚持,而不是跳过整个冲突的提交)。我不想合并,我想让历史线性化。
I've tried:
我试过了:
git rebase -Xtheirs
and
和
git rebase -s 'recursive -Xtheirs'
but git rejects -X
in both cases.
但 git-X
在这两种情况下都拒绝。
git rebase -Xtheirs
works in recent versions, except tree conflicts need to be resolved manually. You need to run git rebase -Xtheirs --continue
(with -X
repeated) after resolving those conflicts.
git rebase -Xtheirs
在最近的版本中工作,除了需要手动解决树冲突。解决这些冲突后,您需要运行git rebase -Xtheirs --continue
(-X
重复)。
回答by iCrazy
You can use this with Git v1.7.3 or later versions.
您可以在 Git v1.7.3 或更高版本中使用它。
git rebase --strategy-option theirs ${branch} # Long option
git rebase -X theirs ${branch} # Short option
(which is a short for git rebase --strategy recursive --strategy-option theirs ${branch}
as stated by the documentation)
(这是文档中git rebase --strategy recursive --strategy-option theirs ${branch}
所述的缩写)
From Git v1.7.3 Release Notes:
来自 Git v1.7.3 发行说明:
git rebase --strategy <s>
learned the --strategy-option
/-X
option to pass extra options that are understood by the chosen merge strategy.
git rebase --strategy <s>
学习了--strategy-option
/-X
选项以传递所选合并策略理解的额外选项。
NB: "Ours" and "theirs" mean the opposite of what they do during a straight merge.In other words, "theirs" favors the commits on the currentbranch.
注意:“我们的”和“他们的”的意思与他们在直接合并期间所做的相反。换句话说,“他们的”有利于当前分支上的提交。
回答by VonC
This is for merge strategies that come with their own set of options
这是用于带有自己的选项集的合并策略
git rebase <branch> -s recursive -X theirs
should work, although this patch mentions(February 2010):
应该可以工作,尽管这个补丁提到(2010 年 2 月):
The manpage says that
git-rebase
supports merge strategies, but the rebase command doesn't know about-X
, and gives the usage when presented with it.
手册页说它
git-rebase
支持合并策略,但 rebase 命令不知道-X
,并在出现时给出了用法。
So if it still doesn't work, it is being debated right now!
(supported in recent git)
所以如果它仍然不起作用,现在正在辩论!
(在最近的 git 中支持)
Update from commit db2b3b820e2b28da268cc88adff076b396392dfe(July 2013, git 1.8.4+),
从提交 db2b3b820e2b28da268cc88adff076b396392dfe(2013 年 7 月,git 1.8.4+)更新,
Do not ignore merge options in interactive rebase
Merge strategy and its options can be specified in
git rebase
, but with-- interactive
, they were completely ignored.Signed-off-by: Arnaud Fontaine
不要忽略交互式变基中的合并选项
可以在 中指定合并策略及其选项
git rebase
,但是使用 时-- interactive
,它们会被完全忽略。签字人:Arnaud Fontaine
That means -X
and strategy now work with interactive rebase, as well as plain rebase.
这意味着-X
和策略现在可以与交互式 rebase 以及普通 rebase 一起使用。
回答by MestreLion
As iCrazysaid, this feature is only available for git 1.7.3 onwards. So, for the poor souls (like me) still using 1.7.1, I present a solution I did myself:
正如iCrazy所说,此功能仅适用于 git 1.7.3 以上。因此,对于仍在使用 1.7.1 的可怜人(如我),我提出了一个我自己做的解决方案:
It is a very well-polished (and thus long) script, meant for production use: ui options, handles multiple files, check if file actually has conflict markers, etc, but the "core" could be summarized in 2 lines:
这是一个非常完善(因此很长)的脚本,用于生产用途:ui 选项、处理多个文件、检查文件是否确实有冲突标记等,但“核心”可以总结为 2 行:
cp file file.bak
awk '/^<+ HEAD$/,/^=+$/{next} /^>+ /{next} 1' file.bak > file
And here is the full script:
这是完整的脚本:
#!/bin/bash
#
# git-rebase-theirs - Resolve rebase conflicts by favoring 'theirs' version
#
# Copyright (C) 2012 Rodrigo Silva (MestreLion) <[email protected]>
#
# 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" "" >&2 ; }
skip() { message "skipping ${2:-$file}${1:+: }"; continue ; }
argerr() { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
invalid() { argerr "invalid option: " ; }
missing() { argerr "missing${1:+ } operand." ; }
usage() {
cat <<- USAGE
Usage: $myname [options] [--] FILE...
USAGE
if [[ "" ]] ; 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) <[email protected]>
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
USAGE
exit 0
}
myname="${0##*/}"
# Option handling
files=()
while (( $# )); do
case "" in
-h|--help ) usage ;;
-v|--verbose ) verbose=1 ;;
--no-backup ) backup=0 ;;
--in-place ) inplace=1 ;;
--in-place=* ) inplace=1
suffix="${1#*=}" ;;
-* ) invalid "" ;;
-- ) shift ; break ;;
* ) files+=( "" ) ;;
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