Search code examples
gitrebasesquashgit-interactive-rebasegit-squash

Combining non-linear commits into one commit (sausage making)


Consider the following git history:

* 21f05f9 - Fifth commit
* 0534049 - Fourth commit
* 738ae0a - Third commit
* 288ffd2 - Second commit
* 2535dca - First commit

How would I combine 21f05f9 738ae0a and 2535dca into one commit?

I have tried git rebase -i 2535dca, picking 2535dca and squashing 21f05f9 and 738ae0a, but I was given the following error:

error: could not apply 21f05f9 ... Fifth commit

I'm not sure if there's a way to skip commits 0534049 and 288ffd2 within rebase's interactive mode.

Is there a better way to do what I am doing? Are there any pitfalls?

Edit:

What I'm beginning to understand is I shouldn't really put myself in a place where non-linear commits are needed, especially if those commits all relate to one file. I could see an instance where a commit could break code or give a conflict.


Solution

  • This might not be as simple if the commits affect the same files. You can try:

    Checkout a new branch from the 1st commit id mentioned:

    git checkout -b new_branch 2535dca
    

    Then just cherrypick the commits you want to add:

    git cherry-pick 738ae0a
    
    git cherry-pick 21f05f9