2016-03-08 57 views
1

我在git中有一个私人分支,我一段时间都没碰过。我想变基它的master顶部,所以我:git rebase尽可能接近HEAD?

git checkout master 
git pull 

git checkout my-branch 
git rebase master 

First, rewinding head to replay your work on top of it... 
Applying: a bunch of stuff 
Applying: more stuff 
Applying: and so on 
CONFLICT (content): Merge conflict in src/foo.c 
error: Failed to merge in the changes. 

哦,不,我不能直接变基到HEAD

git rebase --abort # sigh 

我最常做的在这一点上是试图“棘轮”我的分支接近HEAD成为可能,通过使用git rebase SHA1git rebase SHA2(其中SHA1SHA2等上master ),直到我找到最近的成功重组点。

我选择候选重定点的常用技巧是在每次合并掌握后立即尝试。

有没有一个很好的方法来自动化这个过程?

+0

什么好处,你从基础重建的SHA1/2得到什么?您最终必须在主人身上重新绑定,并且您将得到相同的冲突。 –

+2

只需修复冲突和'git rebase --continue'。 – choroba

+0

如果在rebase中成功合并,它们会被应用,并且我在分支上看到的提交(和潜在的冲突)更少。 –

回答

0

我不相信Git拥有你正在寻找的开箱即用的东西。

但是,您可以使用脚本和Git别名来创建一种“模糊重建”命令。这将会尝试对另一个裁判进行rebase,如果失败了,它会继续向后移动它的后代树,直到它找到一个可以干净地重新摆放的裁判。

这里是一个Perl脚本:

#!/usr/bin/perl -w 

# get ref 
my $ref=shift @ARGV; 
if (!defined $ref) { 
    warn "need ref to rebase against!\n"; 
    exit 1; 
} 

# attempt rebase 
`git rebase $ref 2>&1`; 

# detect if conflict occurred 
# if so, abort the rebase 
# and try again with the rebase target's parent 
my @result = `git ls-files --unmerged`; 
if (scalar @result > 0) { 
    print "failed at $ref\n"; 
    `git rebase --abort 2>&1`; 
    system("$0 $ref~"); 
} else { 
    print "done at $ref\n"; 
} 

比方说,你在~/scripts/fuzzyRebase.pl有这个。定义一个Git别名是这样的:

git config --global alias.fuzzyRebase !/~/scripts/fuzzyRebase.pl 

现在,每当你想调用你的模糊底垫中,你可以使用:

git fuzzyRebase master 
+0

我会放弃这一想法,但实际上想到了这一点,在面向合并到主控的过程中变得复杂起来,因为在向后走并合并时“主”没有什么特别的特殊之处。嗯... –

+0

@RogerLipscombe同意......我认为这里的主要观点是,你正在寻找的东西需要一些脚本,而且这种脚本可能会变得复杂,像merge merge等各种边缘情况 –

+0

在这里一注意,你不必'git ls-files --unmerged',成功时'git rebase'的退出状态为0,冲突时为1(通常非零,但实际上为1)。 – torek