Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase: Combine non-subsequent commits

Tags:

git

git-rebase

by now, I know that there is a nice way how to combine commits and change the commit message by using 'git rebase --interactive'

Having the following situation:

$ git rebase --interactive HEAD^^^^
pick 5b7c140 commitA
pick 40ffd7c commitB
pick 5e7647d commitC
pick 78bea2d commitD

Rebase [...]

Is there also a possibility to handle the following requirements:

Combining commitA and commitC and commitB and commitD to new commits cAC and cBD?

like image 588
John Rumpel Avatar asked Mar 14 '13 09:03

John Rumpel


People also ask

How do I merge commits to a commit?

In the list of branches, select the branch that has the commits that you want to squash. Click History. Select the commits to squash and drop them on the commit you want to combine them with. You can select one commit or select multiple commits using Command or Shift .

Why you should rebase instead of merge?

But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch. The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge .


1 Answers

It is possible - you can also rearrange the order of the commits with interactive rebase:

pick 5b7c140 commitA
squash 5e7647d commitC
pick 40ffd7c commitB
squash 78bea2d commitD

or

pick 5b7c140 commitA
fixup 5e7647d commitC
pick 40ffd7c commitB
fixup 78bea2d commitD

The difference between the two is that squash allows you to edit the commit message for the new commits, whereas fixup just throws away the second commit message leaving the preceeding pick commit message in place for the combined commit. (If your editor launches quickly enough, then having the habit of just choosing squash gives you a nice opportunity to review the commit message even if you think you would probably not need to use parts of the message of the fixup commit.)

There is a possibility of rebase conflict, if your commitB and commitC change a same part of a file. Often these can be easily enough sorted out.

like image 138
FooF Avatar answered Oct 19 '22 14:10

FooF