Git: 重做与不同分支的合并

Git: redo merge with a different branch

我有一个长运行 git 分支,我想合并上游分支。不幸的是,我做了 git merge upstream-topic 并花了数小时解决合并冲突,然后才意识到我真的想做 git merge upstream-master

分支有 99% 相同的内容,除了 upstream-topic 有一堆来自合并 upstream-master 的合并提交,我不想让历史永远混乱。有什么方法可以 "redo" 与 upstream-master 合并而不丢失我所有的冲突解决方案吗?

我刚刚发现 git rerere 并且真的希望我已经启用了它:(

如果 upstream-topicupstream-master 这两个分支确实包含相同的内容,只是那些额外的合并提交不同,那么您可以简单地重用合并的 upstream-topic 的内容到解决 upstream-master:

中的合并冲突
# save the current master which merged upstream-topic
git branch merged-topic

# reset master to its original commit
git reset --hard origin/master

# do the merge, getting lots of conflicts
git merge upstream-master

# instead of solving those conflicts again, just use all the contents
# of your already merged topic
git checkout merged-topic -- .

# check the status, resolve the conflicts, and commit
git status
git add -u .
git commit