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

Git merge feature on develop branch to another already existing develop branch

我有一个 git 回购协议(继承的),分支可以调用 "develop"(剩下的问题是 A)。该分支还有 2 个分支,其中父分支称为 "B" 和 "C",其中发生了客户特定的更改。然而,该分支(下面标记为 A)大约有 1 年历史,因此 B 和 C 已经分道扬镳。我正在努力分解客户特定的功能并将更改重新加入到 A 中,因此我们有一个共同的祖先,但还没有。我们有分支工作流功能,所以我创建了一个新分支 "B"(在本例中是 customer1),名为 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" 方式执行此操作并将我的功能更改到多个非 master/develop 分支?我想我也没有尝试过从 D 合并到 C,但我认为这也行不通。

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

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

git cherry-pick 84bc9cb5 f8a1a83e

如果有更多提交,或者如果您想要一种更系统的方法来 select 一堆提交并在 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>