我在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
Run Code Online (Sandbox Code Playgroud)
和
git checkout -m test somecode.js
Run Code Online (Sandbox Code Playgroud)
(我真的很希望合并-m,但它似乎对我不起作用......)
我以为我接近我需要的东西,但后来我意识到它只是快速转发提交意味着它没有合并,它在测试中写了原始文件.
因此,重申一下,如何将特定文件从一个分支合并到另一个分支,而无需在我正在合并使用git的分支中写入文件.
Pet*_*oes 40
我觉得你喜欢用
git checkout -p
Run Code Online (Sandbox Code Playgroud)
在你的情况下
git checkout dev
git checkout -p test somecode.js
Run Code Online (Sandbox Code Playgroud)
并且您可以交互式应用差异.
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
Run Code Online (Sandbox Code Playgroud)
这样你就可以手动从测试分支上的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
Run Code Online (Sandbox Code Playgroud)