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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13711455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:02:13  来源:igfitidea点击:

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

git

提问by jeremy

I have 2 branches in a git repo, lets call them, dev and test. I have changes in a single file, somecode.js. Both branches have changes to somecode.js. The 2 branches have diverged significantly (but manageably) so a straight "merge" is insufficient.

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

I have tried http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/but it doesn't merge the contents of both files. You basically just write over the file instead of actually merging the contents of the file.

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

I have also tried:

我也试过:

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

And

git checkout -m test somecode.js

(I was really hopeful with the -m for merge, but it didn't appear to work for me...)

(我真的很希望 -m 用于合并,但它似乎对我不起作用......)

I thought I was close to what I needed, but then I realized that it just fast-forwarded the commit meaning it didn't merge, it wrote over the original file in test.

我以为我已经接近我需要的东西了,但后来我意识到它只是快进了提交,这意味着它没有合并,它在测试中覆盖了原始文件。

So, to reiterate, how can I merge a specific file from one branch into another branch without just writing over the file in the branch I am merging into using git.

因此,重申一下,如何将一个特定文件从一个分支合并到另一个分支,而无需使用 git 覆盖我正在合并的分支中的文件。

回答by Peter van der Does

I think you like to use

我想你喜欢用

git checkout -p

In your case

在你的情况下

git checkout dev
git checkout -p test somecode.js

And you can interactively apply the diffs.

您可以交互地应用差异。

回答by the.malkolm

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 

This way you'll manually make a three-way merge from somecode.js on test branch into somecode.js on dev branch.

通过这种方式,您将手动从 test 分支上的 somecode.js 到 dev 分支上的 somecode.js 进行三向合并。

Or.. You can create a temporary branch with the changes you want and make a squash merge from it. Is it 'git way' enough? :)

或者..您可以创建一个临时分支,其中包含您想要的更改并从中进行壁球合并。'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

回答by khagler

I think git merge-file is what you're looking for. From the man page:

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