How to split up a git commit

Quick steps

  1. git rebase -i -> mark your commit with edit
  2. git reset HEAD~ (remove the marked commit, but keep its changes)
  3. Make several commits
  4. 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.

  1. 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
  2. Mark the splittable commit: replace pick with edit. 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

  3. Unwrap the commit, i.e. drop the commit, but keep all its file changes:

    git reset HEAD~
  4. 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
  5. Once you're done, commit:

    git commit -m 'My commit message'
  6. Repeat steps 4 & 5 until no changes are left.

  7. Finish by telling git to continue the rebase:

    git rebase --continue
Dominik Schöler over 6 years ago
This website uses short-lived cookies to improve usability.
Accept or learn more