git: squash/fixup earlier commit

前端 未结 6 1167
野的像风
野的像风 2020-12-03 01:42

Suppose you have:

A-B-C

Now your build/test fails. The fix should be merged in A. My current work-flow is like this:

$ git         


        
6条回答
  •  执笔经年
    2020-12-03 01:50

    I created some aliases to make it easier to use the git commit --fixup and git commit --squash commands added in git 1.7. Add these to your ~/.gitconfig:

    [alias]
      fixup = !sh -c 'REV=$(git rev-parse $1) && git commit --fixup $@ && git rebase -i --autosquash $REV^' -
      squash = !sh -c 'REV=$(git rev-parse $1) && git commit --squash $@ && git rebase -i --autosquash $REV^' -
    

    Usage:

    $ git commit -am 'bad commit'
    $ git commit -am 'good commit'
    
    $ git add .          # Stage changes to correct the bad commit
    $ git fixup HEAD^    # HEAD^ can be replaced by the SHA of the bad commit
    

    The bad commit can be several commits back.

提交回复
热议问题