How can I easily fixup a past commit?

前端 未结 12 2329
执笔经年
执笔经年 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:49

    You can create a fixup for a particular file by using this alias.

    [alias]
    ...
    # fixup for a file, using the commit where it was last modified
    fixup-file = "!sh -c '\
            [ $(git diff          --numstat $1 | wc -l) -eq 1 ] && git add $1 && \
            [ $(git diff --cached --numstat $1 | wc -l) -eq 1 ] || (echo No changes staged. ; exit 1) && \
            COMMIT=$(git log -n 1 --pretty=format:"%H" $1) && \
                git commit --fixup=$COMMIT && \
                git rebase -i --autosquash $COMMIT~1' -"
    

    If you have made some changes in myfile.txt but you don't want to put them in a new commit, git fixup-file myfile.txt will create a fixup! for the commit where myfile.txt was last modified, and then it will rebase --autosquash.

提交回复
热议问题