2016-03-08 22 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