繁体   English   中英

如何将一个分支中的特定文件合并到Git中的另一个分支中

[英]How can I merge a specific file from one branch into another branch in Git

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

我觉得你喜欢用

git checkout -p

在你的情况下

git checkout dev
git checkout -p test somecode.js

并且您可以交互式应用差异。

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

我认为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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM