How to modify a specified commit?

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

    Came to this approach (and it is probably exactly the same as using interactive rebase) but for me it's kind of straightforward.

    Note: I present this approach for the sake of illustration of what you can do rather than an everyday alternative. Since it has many steps (and possibly some caveats.)

    Say you want to change commit 0 and you are currently on feature-branch

    some-commit---0---1---2---(feature-branch)HEAD
    

    Checkout to this commit and create a quick-branch. You can also clone your feature branch as a recovery point (before starting).

    ?(git checkout -b feature-branch-backup)
    git checkout 0
    git checkout -b quick-branch
    

    You will now have something like this:

    0(quick-branch)HEAD---1---2---(feature-branch)
    

    Stage changes, stash everything else.

    git add ./example.txt
    git stash
    

    Commit changes and checkout back to feature-branch

    git commit --amend
    git checkout feature-branch
    

    You will now have something like this:

    some-commit---0---1---2---(feature-branch)HEAD
               \
                 ---0'(quick-branch)
    

    Rebase feature-branch onto quick-branch (resolve any conflicts along the way). Apply stash and remove quick-branch.

    git rebase quick-branch
    git stash pop
    git branch -D quick-branch
    

    And you end up with:

    some-commit---0'---1'---2'---HEAD(feature-branch)
    

    Git will not duplicate (although I can't really say to what extent) the 0 commit when rebasing.

    Note: all commit hashes are changed starting from the commit we originally intended to change.

提交回复
热议问题