2012-12-04 81 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

相关问题