How to split last commit into two in Git

前端 未结 10 506
囚心锁ツ
囚心锁ツ 2020-12-07 06:23

I have two working branches, master and forum and I\'ve just made some modifications in forum branch, that I\'d like to ch

10条回答
  •  离开以前
    2020-12-07 07:20

    Since you're cherry-picking, you can:

    1. cherry-pick it with --no-commit option added.
    2. reset and use add --patch, add --edit or just add to stage what you want to keep.
    3. commit the staged changes.
      • To re-use original commit message, you can add --reuse-message= or --reedit-message= options to the commit command.
    4. Blow away unstaged changes with reset --hard.

    Another way, preserving or editing the original commit message:

    1. cherry-pick the original commit as normal.
    2. Reverse the changes you don't want and use add to stage the reversal.
      • This step would be easy if you are removing what you added, but a bit tricky if you're adding what you removed or reversing a change.
    3. commit --amend to effect the reversal on the cherry-picked commit.
      • You'll get the same commit message again, which you can keep or revise as necessary.

提交回复
热议问题