Git:使用Kdiff3合并到另一个分支的分支

时间:2015-09-25 05:42:24

标签: git merge branching-and-merging kdiff3

我熟悉ClearCase,我们可以将文件合并到任何其他分支的分支。这怎么可能在git中? 例如,我有以下结构

master--->branch_2
  |
  |
  V
branch_1

因此branch_1branch_2会有一些独立的开发。我想使用像kdiff3这样的合并工具将branch_2中的一些特定代码合并到branch_1

编辑: 通过比较文件的2个或3个版本(来自相同或不同的分支),Kdiff3有一个很棒的合并选项,突出显示冲突。然后我们可以跳过每个冲突,从文件的一个或多个版本中选择代码,进行一些编辑(如果需要),最后保存文件。当我使用ClearCase时,这个功能对我来说非常方便。有没有办法配置git mergetool与kdiff3以相同的方式工作?

提前致谢

3 个答案:

答案 0 :(得分:1)

站在branch_1你可以简单地cherry-pick从branch_2那里得到你想要的任何内容。

git cherry-pick <some sha1>

您还可以在branch_1中修改您的工作,以便它在branch_2

之后
git rebase --onto branch_2 branch_1

答案 1 :(得分:1)

听起来您想合并branch_2中给定提交的特定部分

如果是这种情况,您可以使用git cherry-pick --no-commit或(-n)来应用branch_2 中提交的更改,而无需创建新提交。从那时起,您可以通过仅暂存部分已修改文件来决定要提交哪些更改。

以下是一个例子:

git checkout branch_1
git cherry-pick -n <some-commit>  # 1. Apply the changes from some commit in branch_2
                                  # without creating a new commit
git reset HEAD                    # 2. Unstage all changes from that commit
git add -p                        # 3. Decide which changes to include in the commit
git commit                        # 4. Commit those changes

答案 2 :(得分:0)

在目标分支上:

git checkout branch1

在引用branch2版本

时编辑branch1代码
git difftool branch2 -- file.txt

照常提交更改

git add file.txt
git commit