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:
-
Find the commit hash with either
git log
orgit reflog
-
Start a rebase with the commit hash replacing
HASH
below
git rebase -i HASH
-
In the rebase edit screen that comes up, find the line with the commit you want to split. Replace
pick
withedit
-
Save and close the rebase edit screen
-
Reset to the previous commit with
git rebase HEAD~
-
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"
- Finish the rebase:
git rebase --continue
Tags: git howto