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 (optionally setting the previous author manually)
  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'
    

    If you're modifying the commit of someone else, please re-set them as author as described here.

  6. Repeat steps 4 & 5 until no changes are left.

  7. Finish by telling git to continue the rebase:

    git rebase --continue
    

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Also see Git: Splitting up changes into several commits

Dominik Schöler
October 29, 2015Software engineer at makandra GmbH
Posted by Dominik Schöler to makandra dev (2015-10-29 15:10)