How can I easily fixup a past commit?

前端 未结 12 2314
执笔经年
执笔经年 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条回答
  •  Happy的楠姐
    2020-12-02 04:36

    What really bothered me about the fixup workflow was that I had to figure out myself which commit I wanted to squash the change into every time. I created a "git fixup" command that helps with this.

    This command creates fixup commits, with the added magic that it uses git-deps to automatically find the relevant commit, so the workflow often comes down to:

    # discover and fix typo in a previously committed change
    git add -p # stage only typo fix
    git fixup
    
    # at some later point squash all the fixup commits that came up
    git rebase --autosquash master
    

    This only works if the staged changes can be unambiguously attributed to a particular commit on the working tree (between master and HEAD). I find that is the case very often for the type of small changes I use this for, e.g. typos in comments or names of newly introduced (or renamed) methods. If this is not the case, it will at least display a list of candidate commits.

    I use this a lot in my daily workflow, to quickly integrate small changes to previously changed lines into commits on my working branch. The script is not as beautiful as it could be, and it's written in zsh, but it has been doing the job for me well enough for a good while now that I never felt the need to rewrite it:

    https://github.com/Valodim/git-fixup

提交回复
热议问题