Break a previous commit into multiple commits

前端 未结 14 2061
渐次进展
渐次进展 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:30

    git rebase --interactive can be used to split a commit into smaller commits. The Git docs on rebase have a concise walkthrough of the process - Splitting Commits:

    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.

    If you are not absolutely sure that the intermediate revisions are consistent (they compile, pass the testsuite, etc.) you should use git stash to stash away the not-yet-committed changes after each commit, test, and amend the commit if fixes are necessary.

提交回复
热议问题