git - 如何在 Git 中删除过去的合并历史记录?
问题描述
我在 git 中进行了错误的合并,并希望从本地和远程存储库之一中删除有关合并的过去历史记录。该图显示了分支和提交历史。
在这里,prod
分支包含一些特定于分支的信息(例如,db.yml
)。我错误地将prod
分支合并到master
. 结果,master
现在的远程存储库包含 C 和 W 之间的所有过去提交。我想要的是保持分支更改的历史记录干净master
。db.yml
prod
在上图中,我想要的是删除指示的棕色线,以便分支中X之前的提交就master
分支而言是提交B master
,而远程存储库master
应该不知道分支 prod
,该分支是在之后分支出来的提交 B。
换句话说,我只想在master
分支中删除从提交 W 到 X 的合并历史记录(已被推送到master
存储库),而我想将提交 C 到 W 保留在prod
分支中(它有一个单独的远程存储库)。在这种情况下,像过去这样的答案git rebase
中解释的简单策略似乎效果不佳。或者,有什么办法吗?
我怎样才能做到这一点?
解决方案
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
如果它也住在那里。
推荐阅读
- c# - 为什么在解析我的 XML 文档时得到空值
- twitter-bootstrap - Bootstrap 6 和 6 未水平对齐
- actionscript - 无法通过引用访问 MC
- python - DJango NoModuleFoundError
- html - 网页抓取。按“显示更多”按钮。“显示更多”按钮后面的标签是什么?
- google-cloud-platform - Beam/Dataflow ReadAllFromParquet 没有读取任何内容,但我的工作仍然成功?
- python - Python Pandas 合并数据框而不重复列
- excel - 如何让我的 VBA 代码遍历工作簿中的所有工作表?
- r - 如何融合或收集基于多列的数据框?
- java - Spring,在一个查询中将一列添加到现有的多对多列表中