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

Git: Merging to a branch from another branch using Kdiff3

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

master--->branch_2
  |
  |
  V
branch_1

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

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

提前致谢

在目标分支上:

git checkout branch1

参考分支2版本编辑分支1代码

git difftool branch2 -- file.txt

照常提交更改

git add file.txt
git commit

站在 branch_1 中,您可以简单地 cherry-pick 从 branch_2.

提交任何您想要的内容
git cherry-pick <some sha1>

您还可以在 branch_1 中对您的工作进行变基,使其位于 branch_2

之后
git rebase --onto branch_2 branch_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