Break a previous commit into multiple commits

前端 未结 14 2094
渐次进展
渐次进展 2020-12-20 13:09

Without creating a branch and doing a bunch of funky work on a new branch, is it possible to break a single commit into a few different commits after it\'s been committed to

14条回答
  •  盖世英雄少女心
    2020-12-20 13:24

    From git-rebase manual (SPLITTING COMMITS section)

    In interactive mode, you can mark commits with the action "edit". However, this does not necessarily mean that git rebase expects the result of this edit to be exactly one commit. Indeed, you can undo the commit, or you can add other commits. This can be used to split a commit into two:

    • Start an interactive rebase with git rebase -i ^, where is the commit you want to split. In fact, any commit range will do, as long as it contains that commit.

    • Mark the commit you want to split with the action "edit".

    • When it comes to editing that commit, execute git reset HEAD^. The effect is that the HEAD is rewound by one, and the index follows suit. However, the working tree stays the same.

    • Now add the changes to the index that you want to have in the first commit. You can use git add (possibly interactively) or git gui (or both) to do that.

    • Commit the now-current index with whatever commit message is appropriate now.

    • Repeat the last two steps until your working tree is clean.

    • Continue the rebase with git rebase --continue.

提交回复
热议问题