Splitting a Commit
Introduction
Splitting a commit in Git allows you to divide changes from a single commit into multiple smaller commits. This can be useful for improving clarity, managing the scope of changes, or refining your project’s history. This guide covers how to split the last commit or a deeper commit in the history.
Basic Terminal
Splitting the Last Commit
- Undo the Last Commit:
- Stage Changes for the First New Commit:
- Create the First New Commit:
- Repeat Staging and Committing:
- Repeat for the second commit and any additional commits.
- Check for Uncommitted Changes:
- Retrieve Stashed Changes (if any):
Splitting a Deeper Commit
- Copy the SHA of the Commit to Split:
- Start an Interactive Rebase:
- Change Action to
edit
for the Target Commit:edit 1f8f3f0 Commit you want to Split
- Undo the Commit to Split:
- Stage and Commit the First Part:
- Repeat for Subsequent Parts:
- Finalize with No Uncommitted Changes:
- Continue the Rebase:
- Retrieve Stashed Changes (if any):
Conclusion
Splitting a commit into multiple commits allows for a cleaner, more manageable history. This is a powerful technique in Git and can be particularly useful for organizing large commits into smaller, more understandable updates. Remember to be cautious when rewriting history, especially in shared branches or public repositories.