Search code examples
gitgit-rebasesquash

Squashing first and third commits (leaving second unsquashed)


Suppose I have the following history in my repo:

git log --oneline    
<3rd sha1> Third commit.
<2nd sha1> Second commit.
<1st sha1> First commit.

How do I squash the third commit to the first one leaving the second untouched?


Solution

  • I have done this:

    1. Cleaned my current changes (stashed).
    2. git rebase -i HEAD~3.
    3. Edited to:

      pick <2nd sha1> Second commit.

      pick <1st sha1> First commit.

      squash <3rd sha1> Third commit.

    4. Solved the coflict: error: could not apply <2nd sha1>... Second commit..

    5. git rebase --continue.
    6. Text editor was fired with the second commit message, I kept it.
    7. Solved the coflict: error: could not apply <1nd sha1>... First commit..
    8. git rebase --continue.
    9. Text editor was fired with the first commit message, I kept it.
    10. Text editor was fired asking for a commit message for squashing the first and third commit, I typed a new message (First commit (squashed).).

    Then it was done.

    git log --oneline:

    git log --oneline    
    <2nd sha1> Second commit.
    <1st sha1> First commit (squashed).
    

    I managed to do it successfully, the conflicts that occurred was supposed to happen and was easy to solve.

    I am pretty satisfied with the result, but I would like to know anyway if there is any better way do achieve this.