Read more

How to split up a git commit

Dominik Schöler
October 29, 2015Software engineer at makandra GmbH

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
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show snapshot
Dominik Schöler
October 29, 2015Software engineer at makandra GmbH
Posted by Dominik Schöler to makandra dev (2015-10-29 15:10)