如何将更改合并到单个文件,而不是合并提交? [英] How do I merge changes to a single file, rather than merging commits?
问题描述
我有两个分支(A 和 B),我想将分支 A 的单个文件与分支 B 的相应单个文件合并.
I have two branches (A and B) and I want to merge a single file from branch A with a corresponding single file from Branch B.
推荐答案
我遇到了同样的问题.准确地说,我有两个分支 A
和 B
具有相同的文件,但在某些文件中具有不同的编程接口.现在独立于两个分支的接口差异的文件f
的方法在分支B
中进行了更改,但更改对两个分支都很重要.因此,我只需要将分支 B
的文件 f
合并到分支 A
的文件 f
中.
I came across the same problem. To be precise, I have two branches A
and B
with the same files but a different programming interface in some files. Now the methods of file f
, which is independent of the interface differences in the two branches, were changed in branch B
, but the change is important for both branches. Thus, I need to merge just file f
of branch B
into file f
of branch A
.
如果我假设所有更改都在两个分支 A
和 B
中提交,那么一个简单的命令已经为我解决了这个问题:
A simple command already solved the problem for me if I assume that all changes are committed in both branches A
and B
:
git checkout A
git checkout --patch B f
第一个命令切换到分支A
,进入我想要合并B
版本的文件f
的地方.第二个命令用B
的HEAD
的f
对文件f
进行修补.您甚至可以接受/丢弃补丁的单个部分.您可以在此处指定任何提交而不是 B
,它不必是 HEAD
.
The first command switches into branch A
, into where I want to merge B
's version of the file f
. The second command patches the file f
with f
of HEAD
of B
. You may even accept/discard single parts of the patch. Instead of B
you can specify any commit here, it does not have to be HEAD
.
社区编辑:如果B
上的f
文件在A
上还不存在,则省略--patch
选项.否则,您将得到无变化".留言.
Community edit: If the file f
on B
does not exist on A
yet, then omit the --patch
option. Otherwise, you'll get a "No Change." message.
这篇关于如何将更改合并到单个文件,而不是合并提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!