非线性合并后如何恢复线性git历史记录? [英] How to restore linear git history after nonlinear merge?

查看:89
本文介绍了非线性合并后如何恢复线性git历史记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很少提交,我不小心在master分支中进行了非线性合并.我有一个总是尝试保持线性历史的习惯,所以现在我想恢复线性.

Few commits ago I accidentally did a nonlinear merge in my master branch. I have a habit of always trying to keep a linear history, so now I would like to restore the linearity.

我制作了一个虚拟仓库,它模拟了我所遇到的真实情况,目的是使其变得更简单.这是它的GitHub链接: https://github.com/ruohola/merge-question

I have made a dummy repo, which simulates the real situation I'm having for the purposes of making this more simple. Here's a GitHub link to it: https://github.com/ruohola/merge-question

这是git log --oneline --graph --date-order的输出:

* 88a4b7e (HEAD -> master, origin/master, origin/HEAD) 11
* 5aae63c 10
*   5506f33 Merge branch 'other'
|\
| * b9c56c9 9
* | 3c72a2a 8
| * 8d2c1ea 7
| * 35f124b 6
* | 7ca5bc1 5
* | b9e9776 4
| * fd83f02 3
|/
* 4fa8b2e 2
* cbdcf50 1

Sourcetree中的图形相同:

Same graph in Sourcetree:

这是mspaint可视化图像,显示了我希望如何使我的主人看起来像:
(本质上应该像我在合并之前要重新设置基准)

And here is a mspaint visualization of how I would like to get my master to look like:
(it should essentially be like I would've rebased before the merge)

我知道这可能不是最佳实践,并且我对重写历史记录的后果很熟悉(尽管没有人在此分支上工作),但仍然希望能够做到这一点.如何做到这一点?

I know that this might not be the best practice and I am familiar with the consequences of rewriting history (no one else is working on this branch though), but would still want to be able to do this. How can this be accomplished?

推荐答案

也许最简单的方法是滥用" git rebase的默认行为.也就是说,如果没有显式地将--preserve-merges传递给git rebase,它将实际上从历史记录中删除所有合并提交.这使我们可以非常轻松地获得所需的结果:

Perhaps the simplest way this can be done is to "abuse" the default behavior of git rebase. That is, without explicitly passing --preserve-merges to git rebase, it will actually remove all merge commits from the history. This allows us to get the desired result extremely easily:

之前:

~/merge-question (master) $ git log --oneline --graph --date-order
* 88a4b7e (HEAD -> master, origin/master, origin/HEAD) 11
* 5aae63c 10
*   5506f33 Merge branch 'other'
|\
| * b9c56c9 9
* | 3c72a2a 8
| * 8d2c1ea 7
| * 35f124b 6
* | 7ca5bc1 5
* | b9e9776 4
| * fd83f02 3
|/
* 4fa8b2e 2
* cbdcf50 1

运行命令:

~/merge-question (master) $ git rebase 3c72a2a
First, rewinding head to replay your work on top of it...
Applying: 3
Applying: 6
Applying: 7
Applying: 9
Applying: 10
Applying: 11

之后:

~/merge-question (master) $ git log --oneline --graph --date-order
* d72160d (HEAD -> master) 11
* 90a4718 10
* 3c773db 9
* ba00ecf 7
* 9e48199 6
* 24376c7 3
* 3c72a2a 8
* 7ca5bc1 5
* b9e9776 4
* 4fa8b2e 2
* cbdcf50 1

此后,只需简单的git push -f origin master,遥控器的历史记录就会恢复为线性.

After this, just a simple git push -f origin master and the remote's history is back to linear.

这篇关于非线性合并后如何恢复线性git历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆