How can I easily fixup a past commit?

前端 未结 12 2336
执笔经年
执笔经年 2020-12-02 03:48

I just read amending a single file in a past commit in git but unfortunately the accepted solution \'reorders\' the commits, which is not what I want. So here\'s my question

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 04:24

    What I do is:

    git add ...           # Add the fix.
    git commit            # Committed, but in the wrong place.
    git rebase -i HEAD~5  # Examine the last 5 commits for rebasing.
    

    Your editor will open with a list of the last 5 commits, ready to be meddled with. Change:

    pick 08e833c Good change 1.
    pick 9134ac9 Good change 2.
    pick 5adda55 Bad change!
    pick 400bce4 Good change 3.
    pick 2bc82n1 Fix of bad change.
    

    ...to:

    pick 08e833c Good change 1.
    pick 9134ac9 Good change 2.
    pick 5adda55 Bad change!
    f 2bc82n1 Fix of bad change. # Move up, and change 'pick' to 'f' for 'fixup'.
    pick 400bce4 Good change 3.
    

    Save & exit your editor, and the fix will be squished back into the commit it belongs with.

    After you've done that a few times, you'll do it in seconds in your sleep. Interactive rebasing is the feature that really sold me on git. It's incredibly useful for this and more...

提交回复
热议问题