pathikrit
pathikrit

Reputation: 33459

Git change history of feature-branch into a new branch?

Someone else can better reword this question, but here is what I want to do:

I have been working on a long lived major-refactor branch B. I have been regularly merging master in and by now branch B is ahead of master by ~200 commits. I am ready to send a pull request now but I want to cleanup my commit history a bit. Basically I want to squash all my ~200 commits into just 3 commits:

And, just so I don't screw it up, I would like to do this history-rewrite on a separate branch from my own branch B and send in that branch as my pull request.

What's the easiest way I can achieve this in git?

Upvotes: 3

Views: 138

Answers (2)

willoller
willoller

Reputation: 7330

Let's assume you are going to merge to master:

  1. Create and checkout a new branch:

    git checkout -b going-to-squash
    
  2. Rebase onto master

    git rebase --squash master
    
  3. Reset to the master branch

    git reset master
    
  4. Now, your entire squashed branch will be in an un-committed state.

    Create your new commits now.

    I would use a gui for this step, but it's perfectly doable on the command line.

Upvotes: 3

VonC
VonC

Reputation: 1325077

A git rebase --interactive (as described in the Pro Git book) wouldn't be practical, as it allows to pick or squash commits (not parts of commits)

Another way might be to generate a giant patch (like git format-patch origin/master) and then parse that file make 3 patch files:

  • one with all the deletetions
  • one with all the additions
  • the rest

You can then create a branch from the the commit branch B originated (see "Reference Git branch start commit"), and apply those three patches.

Upvotes: 1

Related Questions