Daniel Roy Greenfeld

Daniel Roy Greenfeld

About | Articles | Books | Jobs | News | Tags

Splitting Git Commits

Something I periodically do is split up a commit into multiple commits. I am documenting it here where I can find it fast.

I didn't come up with this technique, sources can be found here, here, and here. All credit goes to those sources.

Break up most recent commit

To reset the most recent commit:

git reset HEAD~

This reverts the commit. Commit individual or groups of files in multiple commits.

What if your commit isn't the most recent one?

Use an interactive rebase to commit things further back. Follow these steps:

  1. Find the commit hash with either git log or git reflog

  2. Start a rebase with the commit hash replacing HASH below

git rebase -i HASH
  1. In the rebase edit screen that comes up, find the line with the commit you want to split. Replace pick with edit

  2. Save and close the rebase edit screen

  3. Reset to the previous commit with git rebase HEAD~

  4. Create new commits using the files and writing the messages that go along with them

git add file/to/be/committed.xyz
git commit -m "Enhance the algorithm to support Mars rovers"
  1. Finish the rebase:
git rebase --continue

Tags: git howto
← Back to home