我有两个分支(A和B),我想合并来自分支A的单个文件与来自分支B的相应单个文件。


当前回答

我将这样做

Git format-patch branch_old..branch_new文件

这将为文件生成一个补丁。

在目标branch_old上应用补丁

去吧等等补丁

其他回答

你可以用:

git merge-file

提示:https://www.kernel.org/pub/software/scm/git/docs/git-merge-file.html

我将这样做

Git format-patch branch_old..branch_new文件

这将为文件生成一个补丁。

在目标branch_old上应用补丁

去吧等等补丁

您可以签出要合并的文件的旧版本,将其保存在不同的名称下,然后在两个文件上运行合并工具。

eg.

B:src/common/storeTs > /tmp/store。ts(其中B是分支名称/提交/标记)

·梅尔德src /普通专卖店。ts / tmp / store ts。

我发现这种方法简单而有用:如何“合并”来自另一个分支的特定文件

事实证明,我们太努力了。我们的好朋友git checkout是完成这项工作的合适工具。 Git checkout source_branch <paths>… 我们可以简单地给git checkout特性分支A的名称和我们想要添加到主分支的特定文件的路径。

为了更好的理解,请阅读全文

这使用git的内部扩散工具。 也许有一点工作要做,但是直截了当。

#First checkout the branch you want to merge into
git checkout <branch_to_merge_into>
    
#Then checkout the file from the branch you want to merge from
git checkout <branch_to_merge_from> -- <file> 
    
#Then you have to unstage that file to be able to use difftool
git reset HEAD <file> 

#Now use difftool to chose which lines to keep. Click on the mergebutton in difftool
git difftool

#Save the file in difftool and you should be done.