Search code examples
gitrebasegit-commit

How to fixup commits to upper in Git?


When rebasing in Git we usually can join (fixup, squash) several commits to bottom one, for instance, A, B, C -> A' (here B and C are newer than A). This is not a good practise as you see a date of beginning of work (commit A), but not it's finish (commit C). Like you made a feature on 23.09.2019, but not on 25.09.

Now we have several commits and want to fixup A, B, C to C: A, B, C, D, E -> C', D', E'. How to do this?


Solution

  • I'd proceed like this

    # We first point to C
    git checkout C
    
    # Then we "unpack" changes between A and C into the working tree
    git reset --soft A
    
    # Let's now create commit C'
    git commit -m "Message for C' (so A,B and C)"
    
    # and finally we just have to bring back copies of D and E
    git cherry-pick D E