Quick steps
-
git rebase -i
-> mark your commit withedit
-
git reset HEAD~
(remove the marked commit, but keep its changes) - Make several commits (optionally setting the previous author manually)
git rebase --continue
Detailed instructions
Basically, you will review the last n
commits and stop at the splittable commit. Then you'll undo that commit and put its changes into new commits at your liking.
-
Review commits (
rebase
)git rebase -i HEAD~3 # or git rebase -i origin/master
Git will give you a list of commits, youngest at the bottom. It will look like this:
pick 27f2769 Do something pick b891fd1 Do many things pick da3c35c Do something else
-
Mark the splittable commit: replace
pick
withedit
. Save/:wq
/exit.Git will now re-apply the commits one by one. Once it has applied the splittable commit (say,
b891fd1
), it will stop and wait for you to make changes -
Unwrap the commit, i.e. drop the commit, but keep all its file changes:
git reset HEAD~
-
Prepare the first new commit by adding files or parts of files:
# Examples: git add vendor # or git add config/database.yml # or git add -p
-
Once you're done, commit:
git commit -m 'My commit message'
If you're modifying the commit of someone else, please re-set them as author as described here.
-
Repeat steps 4 & 5 until no changes are left.
-
Finish by telling git to continue the rebase:
git rebase --continue