How to modify a specified commit?

后端 未结 16 940
清酒与你
清酒与你 2020-11-22 01:53

I usually submit a list of commits for review. If I have the following commits:

  1. HEAD
  2. Commit3
  3. Commit2
16条回答
  •  时光说笑
    2020-11-22 02:51

    If for some reason you don't like interactive editors, you can use git rebase --onto.

    Say you want to modify Commit1. First, branch from before Commit1:

    git checkout -b amending [commit before Commit1]
    

    Second, grab Commit1 with cherry-pick:

    git cherry-pick Commit1
    

    Now, amend your changes, creating Commit1':

    git add ...
    git commit --amend -m "new message for Commit1"
    

    And finally, after having stashed any other changes, transplant the rest of your commits up to master on top of your new commit:

    git rebase --onto amending Commit1 master
    

    Read: "rebase, onto the branch amending, all commits between Commit1 (non-inclusive) and master (inclusive)". That is, Commit2 and Commit3, cutting the old Commit1 out entirely. You could just cherry-pick them, but this way is easier.

    Remember to clean up your branches!

    git branch -d amending
    

提交回复
热议问题