Magit makes this very easy; although you will need to consider one commit at a time.
In the magit status buffer, with all changes unstaged, expand all of the unstaged changes with TAB
, and then use n
and p
to navigate through those changed files/hunks.
Now, considering commit A, for every unstaged hunk that is relevant to A type s
to stage that hunk. If you need to split a hunk it's generally easiest to mark a region of the hunk (set the mark with C-SPC
) and then s
will stage only that marked region.
(Alternatively, -
and +
tell git to produce smaller or bigger hunks respectively, but in most cases I find magit's region handling feature far more efficient.)
Work your way through the changes until you have staged everything required for commit A, and then commit it. You can first expand all of the staged changes with TAB
to verify what you are about to commit1. Unstaging with u
works the same as s
, if you accidentally2 staged something not wanted in commit A.
Now simply repeat the process to build the other commits.
1 Equivalent to git diff --cached
. I highly recommend making this review step a habit for every commit you make.
2 or indeed intentionally -- at times it's simpler to stage a large thing, and then unstage selected bits of it.