git:重写历史:重新排序和合并提交

发布于 2024-09-16 20:36:29 字数 724 浏览 6 评论 0原文

当前情况:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-09-23 20:36:29

您想要进行交互式变基

在尝试 git 命令时,我喜欢做的第一件事是制作我的分支的备份副本:

$ git branch my-backup-branch

假设您的 A1 提交的哈希值为 152274b。试试这个:

$ git rebase -i 152274b^

该命令将打开你的编辑器(通常是 vim),显示如下内容:

pick 152274b A1
pick 14b0838 B1
pick 661b993 A2
pick a6510db B2
pick 557e171 A3
pick 85da0e4 B3

# Rebase 39a47e4..85da0e4 onto 39a47e4
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

从这里你可以更改提交的顺序,完全删除它们,甚至压缩它们。在此示例中,您可能想要像这样移动和挤压:

pick 152274b A1
squash 661b993 A2
squash 557e171 A3
pick 14b0838 B1
squash a6510db B2
squash 85da0e4 B3

尝试一下。如果您最终处于不想要的状态,您可以随时返回到备份分支中保存的状态:

$ git reset --hard my-backup-branch

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:

$ git branch my-backup-branch

Let's say that your A1 commit has a hash of 152274b. Try this:

$ git rebase -i 152274b^

That command will bring up your editor (usually vim) with something like this:

pick 152274b A1
pick 14b0838 B1
pick 661b993 A2
pick a6510db B2
pick 557e171 A3
pick 85da0e4 B3

# Rebase 39a47e4..85da0e4 onto 39a47e4
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

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:

pick 152274b A1
squash 661b993 A2
squash 557e171 A3
pick 14b0838 B1
squash a6510db B2
squash 85da0e4 B3

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:

$ git reset --hard my-backup-branch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文