Search code examples
gitgithubgit-rebasegit-squash

Using git rebase, how to squash a commit into a non-previous commit


I know that using git rebase -i, I could squash a commit which will meld the commit message into the immediate previous commit.

https://github.com/wprig/wprig/wiki/How-to-squash-commits

But using git rebase -i, is it possible to squash a commit into not the previous commit but a commit even older? Lets say I want to squash a commit with a commit that is 3 commits prior to the commit being squashed. If yes, then how?


Solution

  • when you use git rebase -i you are able to reorder the commit if you want to a new order.

    If you e.g. do git rebase -i HEAD~5 and end with this:

      pick bfddbf6 first  commit
      pick 74b19b1 second commit
      pick 03892e7 third  commit
    > pick 0fdc12c fourth commit
      pick 9e422a0 fifth  commit
    

    you can reorder the fifth commit as second to squash it into the first:

      pick bfddbf6 first  commit
    > squash 0fdc12c fourth commit
      pick 74b19b1 second commit
      pick 03892e7 third  commit
      pick 9e422a0 fifth  commit