git:重写历史:重新排序和合并提交
当前情况:
origin/mybranch --- A1 --- B1 --- A2 --- B2 --- A3 --- B3 mybranch
我想清理该历史记录(A1 到 B3),尤其是。因为我还没有把它推送到任何地方,并且因为我想准备一个只有那些 B* 的补丁。
我想要的是:
origin/mybranch --- A1+A2+A3 --- B1+B2+B3 mybranch
我可能根本不会推动这个(或者如果我愿意,只推动 B* 的总和,我必须完全删除 A* 提交),当我进一步处理这个问题时,我可能会得到额外的此类提交,即像这样:
origin/mybranch --- A1+A2+A3 --- B1+B2+B3 --- A4 --- B4 mybranch
然后我想像上面一样重写它。
我不只是想知道任何方法来做到这一点(因为我能够以一种有点黑客的方式得到某种东西),我特别是。在这里询问正确/最好/最干净/最简单的方法来做到这一点。
我在做什么,特别是。是:我正在官方 xorg-xserver-1.7 分支上工作,想要准备一个补丁(B*)。因为我希望能够将我的自编译的xserver替换为系统的xserver以进行简单的测试,所以我应用了一堆Debian/Ubuntu补丁(A*)。但是,当我要将该补丁发布到某个地方时,我想排除这些补丁。
Current situation:
origin/mybranch --- A1 --- B1 --- A2 --- B2 --- A3 --- B3 mybranch
I want to clean that history up (A1 to B3), esp. since I have not pushed it anywhere yet and because I want to prepare a patch which just those B*.
What I want is:
origin/mybranch --- A1+A2+A3 --- B1+B2+B3 mybranch
I probably will not push this at all (or if I will, only the summed B*, I must remove the A* commits at all) and while I am working further on this, I might get additional such commits, i.e. like this:
origin/mybranch --- A1+A2+A3 --- B1+B2+B3 --- A4 --- B4 mybranch
And I want to rewrite that then again like above.
I don't just want to know any method to do this (because I would be able to get sth like in a somewhat hacky way), I am esp. asking here for the right/best/cleanest/easiest way to do this.
What I am doing esp. is: I am working on the official xorg-xserver-1.7 branch and want to prepare a patch (B*). Because I want to be able to replace my self-compiled xserver with the system one for simple testing purpose, I have applied a bunch of Debian/Ubuntu patches (A*). However, when I am going to post that patch somewhere, I want to exclude those.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要进行交互式变基。
在尝试 git 命令时,我喜欢做的第一件事是制作我的分支的备份副本:
假设您的 A1 提交的哈希值为
152274b
。试试这个:该命令将打开你的编辑器(通常是 vim),显示如下内容:
从这里你可以更改提交的顺序,完全删除它们,甚至压缩它们。在此示例中,您可能想要像这样移动和挤压:
尝试一下。如果您最终处于不想要的状态,您可以随时返回到备份分支中保存的状态:
You want to do an interactive rebase.
The first thing I like to do when trying out git commands is to make a backup copy of my branch:
Let's say that your A1 commit has a hash of
152274b
. Try this:That command will bring up your editor (usually vim) with something like this:
From here you can change the order of the commits, delete them entirely and even squash them. In this example, you probably want to move and squash like this:
Give it a try. If you end up in a state that you didn't want, you can always go back to the state you saved in your backup branch: