Search code examples
gitsquash

How can I squash commits between A and K into one commit Z


If I have a commit history like this:

A-B-C-------G-H-J-K   (master)
     \     /
      D-E-F

how can I squash commits between A and K into one commit Z:

A-Z-K   (master)

?


Solution

  • First, execute:

    git rebase -i A
    

    This will show a list of commits in a text editor, starting with B and ending with K.
    You will have to change the text pick in front of commits C, D, E, F, H and J to s or squash. Do not change pick in front of B or K. Please note that the commit G should be missing, because it is a merge commit.

    Finally, save and exit the editor. This will start the actual rebasing.

    The result will be this:

    A-Z-K'                   (master)
     \
      B-C-------G-H-J-K      (no branch)
         \     /
          D-E-F
    

    The part that is on no branch will eventually be removed by a garbage collection.