首页 > 解决方案 > 如何在 Git 中删除过去的合并历史记录?

问题描述

我在 git 中进行了错误的合并,并希望从本地和远程存储库之一中删除有关合并的过去历史记录。该图显示了分支和提交历史。

在此处输入图像描述

在这里,prod分支包含一些特定于分支的信息(例如,db.yml)。我错误地将prod分支合并到master. 结果,master现在的远程存储库包含 C 和 W 之间的所有过去提交。我想要的是保持分支更改的历史记录干净masterdb.ymlprod

在上图中,我想要的是删除指示的棕色线,以便分支中X之前的提交就master分支而言是提交B master,而远程存储库master应该不知道分支 prod,该分支是在之后分支出来的提交 B。

换句话说,我只想在master分支中删除从提交 W 到 X 的合并历史记录(已被推送到master存储库),而我想将提交 C 到 W 保留在prod分支中(它有一个单独的远程存储库)。在这种情况下,像过去这样的答案git rebase中解释的简单策略似乎效果不佳。或者,有什么办法吗?

我怎样才能做到这一点?

标签: gitgithubmergeversion-controlrebase

解决方案


git checkout -B master B     # re-hang the `master` label on the commit before the oops
git cherry-pick Y            # make the history it should have
git checkout -B prod W       # likewise with `prod`: back to before the oops
git merge master             # make the history it should have

除非您习惯于进行通常被称为“邪恶合并”的操作,即混入不相关更改的合并,否则您根本不想保留 X。prod这是从to的合并master

git push --force-with-lease origin master   # overwrite if nothing's changed since

同样,prod如果它也住在那里。


推荐阅读