Skip to content

Git Split a Commit into Multiple Fix

DodaTech Updated 2026-06-24 3 min read

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 -p to stage specific hunks within a file
  • Commit frequently with git commit for each completed subtask
  • Use git status to review what changed before committing
  • Write commit messages that start with the category: "feat:", "fix:", "refactor:"

Common Mistakes with split commit

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to 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

### How do I split a commit that is already pushed?

The same rebase process applies, but you must force push: git push --force-with-lease. Coordinate with your team first — splitting pushed commits rewrites history and affects everyone who has pulled the branch.

Can I split a single file across multiple commits?

Yes. Use git add -p filename to interactively select which hunks to stage. Stage the first logical change, commit it with git commit, then repeat for the remaining hunks. Each hunk becomes part of a different commit.

What if I accidentally split a commit incorrectly?

Use git reflog to find the state before the rebase. Run git reset --hard HEAD@{N} to restore the original state. Then start the split again with a clearer plan for which files belong to which logical change.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro