Question

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:

  • Commit 1 = All files that got deleted
  • Commit 2 = All new added files
  • Commit 3 = Everything else i.e. all moved/editted files

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top