我正在一个新项目中使用Git,该项目有两个并行但目前处于试验阶段的开发分支:

master:导入现有的代码库,再加上一些我通常确信的修改exp1:实验分支#1exp2:实验分支#2

exp1和exp2表示两种非常不同的体系结构方法。在我进一步相处之前,我无法知道哪一个(如果有的话)会起作用。当我在一个分支中取得进展时,我有时会对另一个分支进行有用的编辑,并希望仅合并这些编辑。

将一个开发分支中的选择性更改合并到另一个分支中,同时保留其他所有内容的最佳方法是什么?

我考虑过的方法:

gitmerge——无需提交,然后手动取消大量编辑,我不想在分支之间共享这些编辑。手动将通用文件复制到临时目录中,然后git checkout移动到另一个分支,然后更多地手动将临时目录复制到工作树中。在上述基础上的变化。暂时放弃exp分支,使用另外两个本地存储库进行实验。这使得手动复制文件更加简单。

所有这三种方法都显得乏味且容易出错。我希望有更好的方法;类似于过滤器路径参数,可以使gitmerge更有选择性。


当前回答

还有另一条路要走:

git checkout -p

它是git checkout和git add-p的混合体,可能正是您想要的:

   -p, --patch
       Interactively select hunks in the difference between the <tree-ish>
       (or the index, if unspecified) and the working tree. The chosen
       hunks are then applied in reverse to the working tree (and if a
       <tree-ish> was specified, the index).

       This means that you can use git checkout -p to selectively discard
       edits from your current working tree. See the “Interactive Mode”
       section of git-add(1) to learn how to operate the --patch mode.

其他回答

奇怪的是,git仍然没有这么方便的工具“开箱即用”。我在通过从当前版本分支中修复一些错误来更新一些旧版本分支(仍然有很多软件用户)时大量使用它。在这种情况下,通常需要从主干中的文件中快速获取一些代码行,而忽略许多其他更改(这些更改不应该进入旧版本)。。。当然,在这种情况下需要交互式三路合并,git checkout--patch<branch><file path>不能用于这种选择性合并。

您可以轻松完成:

只需将此行添加到global.gitconfig或local.git/config文件的[alias]部分:

[alias]
    mergetool-file = "!sh -c 'git show $1:$2 > $2.theirs; git show $(git merge-base $1 $(git rev-parse HEAD)):$2 > $2.base; /C/BCompare3/BCompare.exe $2.theirs $2 $2.base $2; rm -f $2.theirs; rm -f $2.base;' -"

这意味着您使用Beyond Compare。如果需要,只需更换您选择的软件即可。或者,如果不需要交互式选择性合并,可以将其更改为三向自动合并:

[alias]
    mergetool-file = "!sh -c 'git show $1:$2 > $2.theirs; git show $(git merge-base $1 $(git rev-parse HEAD)):$2 > $2.base; git merge-file $2 $2.base $2.theirs; rm -f $2.theirs; rm -f $2.base;' -"

然后这样使用:

git mergetool-file <source branch> <file path>

这将为您提供其他分支中任何文件的真正选择性树形合并机会。

简单的方法是,实际上合并来自两个分支的特定文件,而不仅仅是用另一个分支的文件替换特定文件。

第一步:区分分支

git diff branch_b>my_patch_file.patch

创建当前分支和branch_b之间差异的修补程序文件

第二步:对匹配模式的文件应用补丁

git apply-p1--include=pattern/匹配/the/path/to/file/或/folder my_patch_file.patch

关于选项的有用说明

您可以在include模式中使用*作为通配符。

斜杠不需要逃避。

此外,您可以改用--exclude并将其应用于除与模式匹配的文件之外的所有文件,或使用-R反转补丁

-p1选项是*Unix修补程序命令的一个保留,并且事实上,修补程序文件的内容在每个文件名前面加上/或b/(或更多,取决于修补程序文件是如何生成的),您需要删除这些文件,这样它就可以找出真正的文件到需要应用修补程序的文件的路径。

查看git应用程序的手册页以了解更多选项。

第三步:没有第三步

很明显,你想提交你的更改,但谁能说你在提交之前没有其他相关的调整。

tl;博士

git checkout source_branch -- path/to/file
# resolve conflicts if any
git commit -am '...'

我遇到了和你上面提到的完全相同的问题。但我在解释答案时发现这一点更清楚。

摘要:

检查要合并的分支的路径,$git签出source_branch--<paths>。。。提示:它也可以像链接文章中看到的那样,不使用“--”。或者选择性地合并大块$git checkout-p source_branch--<paths>。。。

或者,使用reset,然后添加选项-p,

    $ git reset <paths>...
    $ git add -p <paths>...

最后提交$gitcommit-m“'合并'这些更改”

这是我合并选定文件的工作流。

# Make a new branch (this will be temporary)
git checkout -b newbranch

# Grab the changes
git merge --no-commit  featurebranch

# Unstage those changes
git reset HEAD
(You can now see the files from the merge are unstaged)

# Now you can chose which files are to be merged.
git add -p

# Remember to "git add" any new files you wish to keep
git commit

我遇到了和你上面提到的完全相同的问题。但我发现这个Git博客在解释答案时更清晰。

来自以上链接的命令:

# You are in the branch you want to merge to
git checkout <branch_you_want_to_merge_from> <file_paths...>