How to modify a specified commit?

后端 未结 16 1204
清酒与你
清酒与你 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:28

    Interactive rebase with --autosquash is something I frequently use when I need to fixup previous commits deeper in the history. It essentially speeds up the process that ZelluX's answer illustrates, and is especially handy when you have more than one commit you need to edit.

    From the documentation:

    --autosquash

    When the commit log message begins with "squash! …​" (or "fixup! …​"), and there is a commit whose title begins with the same …​, automatically modify the todo list of rebase -i so that the commit marked for squashing comes right after the commit to be modified

    Assume you have a history that looks like this:

    $ git log --graph --oneline
    * b42d293 Commit3
    * e8adec4 Commit2
    * faaf19f Commit1
    

    and you have changes that you want to amend to Commit2 then commit your changes using

    $ git commit -m "fixup! Commit2"
    

    alternatively you can use the commit-sha instead of the commit message, so "fixup! e8adec4 or even just a prefix of the commit message.

    Then initiate an interactive rebase on the commit before

    $ git rebase e8adec4^ -i --autosquash
    

    your editor will open with the commits already correctly ordered

    pick e8adec4 Commit2
    fixup 54e1a99 fixup! Commit2
    pick b42d293 Commit3
    

    all you need to do is save and exit

提交回复
热议问题