40

我在 git repo 中有 2 个分支,让我们调用它们,开发和测试。我在单个文件 somecode.js 中有更改。两个分支都对 somecode.js 进行了更改。这两个分支已经显着分歧(但可以管理),因此直接“合并”是不够的。

我试过http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/但它没有合并两个文件的内容. 您基本上只是覆盖文件而不是实际合并文件的内容。

我也试过:

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 的分支中写入文件。

4

3 回答 3

41

我想你喜欢用

git checkout -p

在你的情况下

git checkout dev
git checkout -p test somecode.js

您可以交互式地应用差异。

于 2012-12-04T21:40:17.380 回答
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方式'足够吗?:)

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
于 2012-12-04T21:07:40.853 回答
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.
于 2012-12-04T20:56:59.357 回答