3

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?

pathikrit
  • 32,469
  • 37
  • 142
  • 221

2 Answers2

3

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.

willoller
  • 7,106
  • 1
  • 35
  • 63
  • 1
    see those answers for step 4: https://stackoverflow.com/a/51972071/946789 (VSCode commit selected ranges) or https://stackoverflow.com/a/1085191/946789 (`git add -patch`, staging only "hunks") – c69 May 07 '21 at 04:28
1

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250