git - 如何跨 merge 提交移动提交?

标签 git git-merge git-rebase revision-history

假设我有这样一个存储库:

I --- C --- M    master
  \       /
   `- A -´       topic

其中 M 是将 topic merge 到 master 中的 merge 提交。

后来我在 C 中发现了一个错误,所以我在 master 分支上提交修复它,在 M 之上:

I --- C --- M --- C1    master
  \       /
   `- A -´              topic

但理想情况下,我希望历史看起来像这样:

I --- C --- C1 --- M    master
  \              /
   `- A --------´       topic

如何重写历史,使 C1 出现在 merge M 之前?

我可以删除 M,应用 C1 制作的补丁,然后再次将 topic merge 到 master 中,再次解决所有冲突,但我想避免这种努力,如果可能的话,我更愿意保留原始提交信息(作者、日期等),这排除了执行 git commit 再次。我希望 git rebase 可以实现,但我失败了,-p-i 中的一个或两个都失败了。

最佳答案

这可以通过单个 rebase 来移动提交(或任意数量的提交),然后从主题重做 merge 来完成。

# Create some branches just for readability
git branch mergeCommit master^
git branch beforeMerge master^^

# Run the rebase
git rebase --onto beforeMerge mergeCommit master
# After the rebase master will have the replayed commits on top of C

# Redo the merge
git merge topic

通过这个 rebase ,您可以在 merge 前移动多个提交。你可以阅读 plan english 中的 rebase 指令:

Grab the commits from mergeCommit to master and rebase them on top of beforeMerge.

关于git - 如何跨 merge 提交移动提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21200768/

相关文章:

git-merge - 如何将 git 标签合并到分支上

git - 查找文件首次添加到 Git 存储库的日期/时间

Jenkins 中的 Android Git 标签

git - 在 9 月 30 日 LetsEncrypt 证书到期之前,Ubuntu 20.04 上的 'git' 是否已损坏?

git - 挤压 Git 镜像提交

git - 重命名 merge 的提交而不丢失该提交的父级

git - 如何在不丢失所有提交历史记录的情况下将 repo 迁移到 git-lfs?

git - 设置默认差异算法不会转化为默认 merge 算法(耐心)

git - 如何只保留 git rebase 中的头部更改

git - 在 Git 中编辑根提交?