Search code examples
gitrebasesquash

Squashing past git commits


I have a git history as such:

* aab941c (HEAD -> master) Added ...
| * 2519b79 (participantQueue) Added ...
| * 39c4efb Created ...
|/  
* 87b0cf7 Import ...
| * 5569822 (settings) Modified ...
|/  
* ee67831 Added ...
* c07902f Added ...
* 7f5ab04 Added ...
* 82be721 Modified from `A` to `B`
* d86702b Modified from `A` to `B`
* 8cad721 Modified from `A` to `B`
* 5db240b Removing ...

I want to squash 8cad721 to 82be721, and end up with:

* aab941c (HEAD -> master) Added ...
| * 2519b79 (participantQueue) Added ...
| * 39c4efb Created ...
|/  
* 87b0cf7 Import ...
| * 5569822 (settings) Modified ...
|/  
* ee67831 Added ...
* c07902f Added ...
* 7f5ab04 Added ...
* 82be721 Modified from `A` to `B` (82be721, d86702b squashed into here)
* 5db240b Removing ...

I have tried git rebase -i 5db240b as suggested in other SO answers, replacing pick to squash for d86702b and 82be721. This results in the following:

* 4d91ea0 (HEAD -> master) Added ...
* 7040d3c Import ...
* f6c0fb5 Added ...
* 299c918 Added ...
* 58b209f Added ...
* e8b36f7 Modified from `A` to `B`
| * 2519b79 (participantQueue) Added default participant properties to unpaired queue export
| * 39c4efb Created ...
| * 87b0cf7 Import ...
| | * 5569822 (settings) Modified ...
| |/  
| * ee67831 Added ...
| * c07902f Added ...
| * 7f5ab04 Added ...
| * 82be721 Modified from `A` to `B`
| * d86702b Modified from `A` to `B`
| * 8cad721 Modified from `A` to `B`
|/  
* 5db240b Removing ...

What command should I be using instead?


After implementing the below answer by LeGEC, I have the following:

* 669164c (participantQueue) Added ...
* 5bc13a8 Created ...
| * 6abf940 (settings) Modified ...
| | * 3518be1 (HEAD -> master) Added ...
| |/  
|/|   
* | 2692632 Import ...
|/  
* 810389b Added ...
* 0c85217 Added ...
* 9284cff Added ...
* eee5eef Modified  from `A` to `B`
* 5db240b Removing ...

This threw me for a second as it does look different, but one more commit to master reorders the "trunk" of the git log output as before.


Solution

  • You have to also rewrite the history of participantQueue and settings, so that they fork off the new commits you created for master.

    You can use git rebase --onto ... :

    # for 'settings' :
    git checkout settings
    # rewrite onto f6c0fb5 the history coming after ee67831 :
    git rebase --onto f6c0fb5 ee67831
    
    # for 'participantQueue' :
    git checkout participantQueue
    # rewrite onto 7040d3c the history coming after 87b0cf7 :
    git rebase --onto 7040d3c 87b0cf7
    

    If you had a bigger set of branches or tags to move, you would be better off using a global rewriting tool, such as git filter-repo.

    In your case, with only two extra branches to move, this more manual way will work just as well.