2012-12-04 79 views
38

我在一個git倉庫中有2個分支,我們可以調用它們,進行開發和測試。我在單個文件somecode.js中進行了更改。這兩個分支都改變了一些code.js。這兩個分支已經有很大的分歧(但是很可控),所以直接的「合併」是不夠的。我試過http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/但它不合並兩個文件的內容。你基本上只是在文件上寫而不是實際合併文件的內容。如何將一個分支中的特定文件合併到Git中的另一個分支中

我也曾嘗試:

git checkout -b newbranch 
git checkout test somecode.js 
git commit -m "somecode changes from newbranch" 
git checkout dev 
git merge newbranch 

而且

git checkout -m test somecode.js 

(我是用-m對於合併真的有希望,但它似乎沒有爲我工作...)

我以爲我接近我所需要的,但後來我意識到它只是快速轉發意味着它沒有合併的提交,它在測試中寫入原始文件。

所以,重申一下,我怎樣才能合併一個分支的特定文件到另一個分支,而不必寫在我正在合併到使用git的分支中的文件上。

+0

可能重複:http://stackoverflow.com/questions/449541/how-do-you-merge-selective-files-with-git-merge – rlegendi

+0

摘櫻桃不能解決問題,因爲合併完成提交。所以它會帶來多個文件,我將不得不查看所有提交,以查看哪些是相關的 – jeremy

+0

可能的重複[如何合併對單個文件的更改,而不是合併提交?](http:// stackoverflow .com/questions/10784523/how-do-i-merge-changes-to-a-single-file-rather-merge-commits) –

回答

40

我覺得你喜歡用

git checkout -p 

在你的情況

git checkout dev 
git checkout -p test somecode.js 

而且可以交互應用的diff。

+0

完美!謝謝!! – jeremy

+1

但是你不允許git做任何合併..你手動完成所有的工作。 –

+1

@ the.malkolm在第一個問題中,你用'a'回答,就是這樣。 (a - 在文件中應用此塊和所有後來的hunk) –

1

我認爲git merge-file就是你要找的東西。從手冊頁:

git merge-file incorporates all changes that lead from the <base-file> to 
<other-file> into <current-file>. The result ordinarily goes into <current-file>. 
git merge-file is useful for combining separate changes to an original. Suppose 
<base-file> is the original, and both <current-file> and <other-file> are 
modifications of <base-file>, then git merge-file combines both changes. 
+0

我沒有看到從其他分支指定文件的方法。這看起來可能是內部使用?你能顯示使用情況嗎? – jeremy

+0

'git merge-file'只是文件的三路合併驅動程序。這種描述有點令人誤解。它沒有看到兩者之間的所有提交,它只是做了一個經典的3路合併。 –

5
git checkout dev 
git show test:somecode.js > somecode.js.theirs 
git show $(git merge-base dev test):somecode.js > somecode.js.base 
git merge-file somecode.js somecode.js.base somecode.js.theirs 

這種方式,您將手動進行三路從somecode.js合併上測試分支插入Dev分支somecode.js。

或..您可以使用所需的更改創建一個臨時分支,然後通過它進行壁球合併。這是'git way'嗎? :)

git checkout -b test/filtered $(git merge-base dev test) 
git diff ..test -- somecode.js | git apply 
git add -- somecode.js 
git commit -m "Updated somecode.js" 
git checkout dev 
git merge --squash test/filtered 
git branch -D test/filtered 
+0

我想這是有效的,但我希望有一個「git-way」來做到這一點。我會等待更多的答案,並且如果我無法弄清楚git-way,就批准這個答案。 – jeremy

+0

這是一個'git way'嗎? :) –

+3

我一直希望像「混帳簽出-m測試somecode.js」 – jeremy

相關問題