Git Split a Commit into Multiple Fix
In this tutorial, you'll learn about Git Split a Commit into Multiple Fix. We cover key concepts, practical examples, and best practices.
A single commit contains changes to multiple unrelated features, making code review difficult and rollback risky. Splitting a commit into smaller logical commits lets you keep a clean, atomic history.
The Problem
git log --oneline
Shows:
abc123 Add user login and profile page and fix CSS
This commit includes three unrelated changes: login feature, profile page, and CSS fixes. Reviewers cannot evaluate them separately, and reverting one change reverts all.
Wrong Approach
# WRONG — resetting and losing the commit entirely
git reset --hard HEAD~1
# Loses all the work
Right Approach
git rebase -i HEAD~1
Change pick to edit, then save and exit.
# Unstage everything
git reset HEAD~1
# Stage and commit each logical change separately
git add src/login/
git commit -m "Add user login feature"
git add src/profile/
git commit -m "Add profile page"
git add src/css/
git commit -m "Fix CSS layout issues"
git rebase --continue
Expected output:
[detached HEAD def456] Add user login feature
[detached HEAD ghi789] Add profile page
[detached HEAD jkl012] Fix CSS layout issues
Successfully rebased and updated refs/heads/main.
Step-by-Step Guide
Step 1: Start interactive rebase
git rebase -i HEAD~1
Step 2: Mark the commit for editing
Change pick to edit (or e), save, and exit.
Step 3: Soft reset to unstage everything
git reset HEAD~1
All files from the commit are now unstaged.
Step 4: Stage files by logical group
git add src/login/
git commit -m "Add user login feature"
Step 5: Repeat for each logical group
git add src/profile/
git commit -m "Add profile page"
git add src/css/
git commit -m "Fix CSS layout issues"
Step 6: Continue the rebase
git rebase --continue
Step 7: Verify the split
git log --oneline -5
Prevention Tips
- Make each commit represent one logical change
- Use
git add -pto stage specific hunks within a file - Commit frequently with
git commitfor each completed subtask - Use
git statusto review what changed before committing - Write commit messages that start with the category: "feat:", "fix:", "refactor:"
Common Mistakes with split commit
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro