How to split last commit into two in Git

前端 未结 10 508
囚心锁ツ
囚心锁ツ 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:14

    The double-revert-squash method

    1. Make another commit that removes the unwanted changes. (If it's per file, this is really easy: git checkout HEAD~1 -- files with unwanted changes and git commit. If not, files with mixed changes can be partially staged git reset file and git add -p file as an intermediate step.) Call this the revert.
    2. git revert HEAD – Make yet another commit, that adds back the unwanted changes. This is the double-revert
    3. Of the 2 commits you now made, squash the first onto the commit to split (git rebase -i HEAD~3). This commit now becomes free of the unwanted changes, for those are in the second commit.

    Benefits

    • Preserves the commit message
    • Works even if the commit to split is not the last one. It only requires that the unwanted changes do not conflict with later commits

提交回复
热议问题