2010-04-06 33 views
6

我有一個文件,其中只有一行。 我創建了一個分支並將第二行添加到同一個文件中。保存並提交到分支。 我切換回主人。並在文件中添加不同的第二行。保存並提交給主。 所以總共有3條獨特的線條。爲什麼Git不能將文件更改與修改的父級/主級合併?

如果我現在嘗試並將分支合併回主,它遭受合併衝突。

爲什麼不能Git簡單合併每一行,一個接一個?

我在合併企圖的行爲是這樣的:

PS D:\dev\testing\test1> git merge newbranch 
Auto-merging hello.txt 
CONFLICT (content): Merge conflict in hello.txt 
Automatic merge failed; fix conflicts and then commit the result. 
PS D:\dev\testing\test1> git diff 
diff --cc hello.txt 
index 726eeaf,e48d31a..0000000 
--- a/hello.txt 
+++ b/hello.txt 
@@@ -1,2 -1,2 +1,6 @@@ 
    This is the first line. 
- New line added by master. 
-Added a line in newbranch. 
++<<<<<<< HEAD 
++New line added by master. 
++======= 
++Added a line in newbranch. 
++>>>>>>> newbranch 

有沒有一種方法,使在自動槽線,一個接一個?

+2

似乎沒有一種簡單的方法來確定它們應該被分割的順序 - 畢竟,當涉及到可執行代碼時,順序肯定會很重要。 – Amber 2010-04-06 22:39:28

回答

12

比方說文件分支一個樣子:

First line 
Branch A's second line 

和支路B的樣子:

First line 
Branch B's second line 

當合並,也有解決它兩種不同的方式。這裏有一種方法:

First line 
Branch A's second line 
Branch B's second line 

這裏的另一種方式:

First line 
Branch B's second line 
Branch A's second line 

混帳不知道哪些你喜歡這兩個選項之一者,或者甚至是可以接受的合併。

1

我可以告訴你爲什麼它不能合併兩者 - 根據git,FileA的第2行是「這樣那樣的」,而FileB的第2行是「別的東西」。因此,根據git,它試圖從FileA和FileB創建一個具有兩個可能值的行。它不能決定你想要哪一個,所以它將它們都轉儲並讓你修復它。

2

讓我們說你有這樣一行:

printf(something); 
在branchA

,你把它:

while(x < 5) 
printf(something); 
在branchB

,你把它:

if(y>10) 
printf(something); 

你怎麼能合併這個,或者說,你會相信一個工具爲此創建的合併結果嗎?

4

有一個名爲union的git合併策略,可以從.gitattributes配置文件中配置,它可以爲你做這件事,但是你將無法控制衝突行將在合併文件中產生的順序,沒有衝突標記來指示出現這種情況。

您也可以定義自定義合併策略,該策略可以使用關於文件結構的一些專業知識來確定正確的順序。

相關問題