开发分支上的 Git 合并功能到另一个已经存在的开发分支



我有一个 git 存储库(继承的(和分支让我们调用"开发"(其余问题为 A(。该分支还有 2 个分支,其中它是名为"B"和"C"的父分支,其中发生了客户特定的更改。然而,该分支(下面标记的 A(大约有 1 年的历史,因此 B 和 C 已经分道扬镳。我正在努力分解客户特定的功能并将更改重新加入到 A 中,以便我们有一个共同的祖先,但还没有。我们确实具有分支工作流,因此我在"B"(在本例中是客户 1(创建了一个名为 D 的新分支。

A -> B
-> D
-> C

我在新功能上做了 100 次提交,并做了一个"git co B && git merge D",在这种情况下,它恰好是分支 B 的 100% 新文件(除了 .gitignore(。我没有挤压,我的 git 日志现在看起来像

*   250f8fd4 - (origin/B, origin/HEAD, B) Add new files for project X (3 days ago) <me>
|
| * f8a1a83e - (origin/D, D) cleaning up before merge (3 days ago) <me>
* | 84bc9cb5 - cleaning up before merge (3 days ago) <me>
|/
* 08510627 - variablize and not hardcode value (3 days ago) <me>

然后我git推送,并验证一切正常。我现在希望这些相同的文件位于分支 C 中。由于这些是 100% 的新文件,我可以将它们从 B 复制到 C,但我不希望将来将所有分支折叠回 A 时发生合并冲突。

运行"git merge 250f8fd4"会导致自 B 从 A 分流到 C 以来的所有更改(包括覆盖客户特定的文件和更改(,并生成数千个合并冲突。我使用 git merge --abort 来撤消它。

$ git cherry-pick 250f8fd4
error: commit 250f8fd4e41c069eb1a2861855a4db30a1fba658 is a merge but no -m option was given.
fatal: cherry-pick failed

失败了,所以让我们试着告诉它哪一边

$ git cherry-pick -m 1 250f8fd4
On branch C
Your branch is up to date with 'origin/C'.
You are currently cherry-picking commit 250f8fd4.
nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:
git commit --allow-empty
Otherwise, please use 'git reset'

无论我使用 -m 1 还是 -m 2,它总是导致空提交。

如何以"git"方式执行此操作并将我的功能更改放入多个 NON 主/开发分支中?我想我也没有尝试过从 D 合并到 C 中,但我认为这也行不通。

git

cherry-pick错误消息中暗示了它:提交250f8fd4只是合并提交;实际更改包含在84bc9cb5f8a1a83e中。

只有两个提交,您可以轻松挑选它们:

git cherry-pick 84bc9cb5 f8a1a83e

如果有更多的提交,或者如果你想要一种更系统的方式来选择一堆提交并在C之上重放它们,你可以使用--onto选项git rebase

# this will rebase :
#  - on top of commit C (--onto C)
#  - commits starting from 08510627 (this commit will *not* be included)
#  - up to 250f8fd4 (included)
git rebase --onto C 08510627 250f8fd4
# once the rebase is completed, you can update branch C to this new commit :
git branch -f C
git checkout C
# or :
git checkout C
git merge --ff-only <rebased-commit>

最新更新