git rebase
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 <commit>^
, whereis 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.