Search code examples
gitrebasesquash

How can I rebase as one commit using Git


My history looks somewhat like this but times 10:

                i - j - e'- k - h'- l - m  feature-branch
              /
    a - b - c - d - e - f - g - h  master

(the apostrophes meaning cherry picks) I want to rebase to this:

                                  i - j - k - l - m  feature-branch
                                 / 
    a - b - c - d - e - f - g - h  master

I don't mind if the feature gets squashed into 1 commit. The main problem with a regular rebase is that it tries to rebase one commit at a time, and i have to fix and re-fix similar conflicts over and over.

All I want is to take the difference between the tip of my branch and the tip of master and apply those on top of master.


Solution

  • This is actually quite easy.

    1. Merge master into feature-branch. You will solve all of your merge conflicts here at once. (I highly recommend stopping here.)

                  i - j - e'- k - h'- l - m - n    feature-branch
                /                           /
      a - b - c - d - e - f - g - h --------       master
      
    2. Then, git reset --soft master. This will make it so that feature-branch points at master, but it will keep all of your changes in the index, ready to be committed.

                  i - j - e'- k - h'- l - m - n    (orphaned)
                /                           /
      a - b - c - d - e - f - g - h --------       master, feature-branch
                                    \
                                      (index)
      
    3. git commit

                  i - j - e'- k - h'- l - m - n    (orphaned)
                /                           /
      a - b - c - d - e - f - g - h --------       master
                                    \
                                      n'           feature-branch
      

    The only purpose of #2 and #3 is to destroy the history of feature-branch. If you are certain you will never ever need that history, that's fine. But it seems like a waste to go to all this extra trouble just to delete an accurate record of what actually happened.