开发者

git: rewrite history: reorder and merge commits

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 syst开发者_运维知识库em 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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜