Jez
Jez

Reputation: 30071

Interactive rebase without changing branching point?

I want to do something similar to what the standard interactive rebase does, but I want to use it just to clean up my branch's history and not pull in any updates from another branch. So, I want to keep the 'branching point' the same. For example, if my branch is B1:

--O--O--O--O--O--O------O--O----O--O   master
      \
       O---O--x--x--x--O--O--x--x--O   B1

In the above diagram, O is a clean commit and x is a messy commit I want to squash with the ones before it and maybe change the commit message. With a normal interactive rebase on master I could get this:

--O--O--O--O--O--O------O--O----O--O                        master
                                    \
                                     O---O--X--O--O--X--O   B1

Where X is messy commits squashed together with a cleaned up commit message. But how can I get the below?

--O--O--O--O--O--O------O--O----O--O   master
      \
       O---O--X--O--O--X--O            B1

Upvotes: 1

Views: 197

Answers (2)

GoZoner
GoZoner

Reputation: 70235

The 'git rebase' documentation deals exactly with your case:

A range of commits could also be removed with rebase. If we have the following situation:

           E---F---G---H---I---J  topicA

   then the command

       git rebase --onto topicA~5 topicA~3 topicA

   would result in the removal of commits F and G:

           E---H'---I'---J'  topicA

   This is useful if F and G were flawed in some way, or should not be
   part of topicA. Note that the argument to --onto and the <upstream>
   parameter can be any valid commit-ish.

Upvotes: 0

Benjamin Bannier
Benjamin Bannier

Reputation: 58764

You can rebase on any commit, not just on master's HEAD. Let's say your branch point of B1 is 12345, then you can interactively rebase B1 on that commit with

# make sure you are on B1
$ git checkout B1

$ git rebase -i 12345

Then cleanup the history.

See git help rebase for more pointers.

Upvotes: 3

Related Questions