---
title: How to Split a Messy Git Commit Into Clean Ones
description: Splitting a git commit into clean, reviewable pieces takes an interactive rebase, a soft reset, and git add -p. Here is the exact sequence.
canonical: https://lukeocodes.dev/splitting-a-git-commit
type: article
locale: en_GB
published: 2026-08-15T00:00:00.000Z
modified: 2026-08-15T00:00:00.000Z
authors:
  - Luke Oliff
section: Git
tags:
  - Git
  - TIL
  - rebase
  - git add
  - commit history
  - cli
feed: https://lukeocodes.dev/rss.xml
search: https://lukeocodes.dev/search.json?q={query}
schema:
  - "@context": https://schema.org
    "@graph":
      - "@type": Organization
        "@id": https://ably.com/#organization
        name: Ably
        url: https://ably.com
      - "@type": WebSite
        "@id": https://lukeocodes.dev/#website
        url: https://lukeocodes.dev
        name: Luke Oliff
        alternateName: lukeocodes
        inLanguage: en
        publisher:
          "@id": https://lukeocodes.dev/authors/luke#person
        description: Personal blog and writing by Luke Oliff — developer relations, voice AI, developer experience.
        potentialAction:
          - "@type": SearchAction
            target:
              "@type": EntryPoint
              urlTemplate: https://lukeocodes.dev/blog/search?q={search_term_string}
            query-input: required name=search_term_string
      - "@type": Person
        "@id": https://lukeocodes.dev/authors/luke#person
        name: Luke Oliff
        url: https://lukeocodes.dev/authors/luke
        description: Staff engineer and Staff Tech Lead for Developer Experience at Ably. Writing about developer relations, AI, and the voice industry, from the UK.
        mainEntityOfPage: https://lukeocodes.dev/authors/luke
        givenName: Luke
        familyName: Oliff
        image: https://lukeocodes.dev/authors/luke.jpg
        jobTitle: Staff Tech Lead, Developer Experience
        worksFor:
          "@id": https://ably.com/#organization
        sameAs:
          - https://www.linkedin.com/in/lukeocodes
          - https://github.com/lukeocodes
          - https://bsky.app/profile/lukeocodes.dev
          - https://x.com/lukeocodes
          - https://hashnode.com/@lukeocodes
          - https://dev.to/lukeocodes
        knowsAbout:
          - Developer Relations
          - Developer Experience
          - Voice AI
          - Text-to-Speech
          - Answer Engine Optimisation
          - Search Engine Optimisation
          - Open Source
          - Developer Tooling
          - API Design
          - Speech-to-Text
          - Voice Agents
          - Community Building
        knowsLanguage:
          - English
        nationality: British
        alumniOf:
          - "@type": Organization
            name: The Open University
  - "@context": https://schema.org
    "@graph":
      - "@type": Organization
        "@id": https://ably.com/#organization
        name: Ably
        url: https://ably.com
      - "@type": Person
        "@id": https://lukeocodes.dev/authors/luke#person
        name: Luke Oliff
        url: https://lukeocodes.dev/authors/luke
        description: Staff engineer and Staff Tech Lead for Developer Experience at Ably. Writing about developer relations, AI, and the voice industry, from the UK.
        mainEntityOfPage: https://lukeocodes.dev/authors/luke
        givenName: Luke
        familyName: Oliff
        image: https://lukeocodes.dev/authors/luke.jpg
        jobTitle: Staff Tech Lead, Developer Experience
        worksFor:
          "@id": https://ably.com/#organization
        sameAs:
          - https://www.linkedin.com/in/lukeocodes
          - https://github.com/lukeocodes
          - https://bsky.app/profile/lukeocodes.dev
          - https://x.com/lukeocodes
          - https://hashnode.com/@lukeocodes
          - https://dev.to/lukeocodes
        knowsAbout:
          - Developer Relations
          - Developer Experience
          - Voice AI
          - Text-to-Speech
          - Answer Engine Optimisation
          - Search Engine Optimisation
          - Open Source
          - Developer Tooling
          - API Design
          - Speech-to-Text
          - Voice Agents
          - Community Building
        knowsLanguage:
          - English
        nationality: British
        alumniOf:
          - "@type": Organization
            name: The Open University
      - "@type": BlogPosting
        "@id": https://lukeocodes.dev/splitting-a-git-commit#article
        headline: How to Split a Messy Git Commit Into Clean Ones
        alternativeHeadline: How to Split a Messy Git Commit Into Clean Ones
        description: Splitting a git commit into clean, reviewable pieces takes an interactive rebase, a soft reset, and git add -p. Here is the exact sequence.
        datePublished: 2026-08-15T00:00:00.000Z
        dateModified: 2026-08-15T00:00:00.000Z
        url: https://lukeocodes.dev/splitting-a-git-commit
        mainEntityOfPage: https://lukeocodes.dev/splitting-a-git-commit
        inLanguage: en
        author:
          - "@id": https://lukeocodes.dev/authors/luke#person
        isPartOf:
          "@id": https://lukeocodes.dev/#website
        keywords: Git, TIL, rebase, git add, commit history, cli
        articleSection: Git
        about:
          - "@type": SoftwareApplication
            name: Git
        abstract: You don't fix a messy commit by force. Mark it edit in git rebase -i, run git reset HEAD^ to unstage everything, then rebuild it with git add -p, committing each logical change on its own.
        speakable:
          "@type": SpeakableSpecification
          cssSelector:
            - .post-tldr
            - h1
        image: https://lukeocodes.dev/og.png?h=How+to+Split+a+Messy+Git+Commit+Into+Clean+Ones&d=Splitting+a+git+commit+into+clean%2C+reviewable+pieces+takes+an+interactive+rebase%2C+a+soft+reset%2C+and+git+add+-p.+Here+is+the+exact+sequence.&e=Writing
        wordCount: 639
        timeRequired: PT4M
        publisher:
          "@id": https://ably.com/#organization
      - "@type": BreadcrumbList
        itemListElement:
          - "@type": ListItem
            position: 1
            name: Home
            item: https://lukeocodes.dev
          - "@type": ListItem
            position: 2
            name: Writing
            item: https://lukeocodes.dev/blog
          - "@type": ListItem
            position: 3
            name: How to Split a Messy Git Commit Into Clean Ones
            item: https://lukeocodes.dev/splitting-a-git-commit
      - "@type": FAQPage
        "@id": https://lukeocodes.dev/splitting-a-git-commit#faq
        mainEntity:
          - "@type": Question
            name: How do I split the most recent commit?
            acceptedAnswer:
              "@type": Answer
              text: Run git reset HEAD^ to undo the commit while keeping the changes in your working tree, then stage them in groups with git add -p and commit each group. No rebase needed if it's the last commit.
          - "@type": Question
            name: What does git add -p actually do?
            acceptedAnswer:
              "@type": Answer
              text: It walks you through each change hunk by hunk and asks whether to stage it. You answer y or n per hunk, and can split a hunk further with s. It's how you commit half a file and leave the rest.
          - "@type": Question
            name: Can I split a commit that isn't the latest one?
            acceptedAnswer:
              "@type": Answer
              text: Yes. Run git rebase -i <commit>^, mark the target with edit, and when the rebase stops there do the reset-and-restage dance. Then git rebase --continue replays the commits that came after it.
          - "@type": Question
            name: Is splitting a git commit safe on a shared branch?
            acceptedAnswer:
              "@type": Answer
              text: No. Rewriting history changes commit hashes, so only do it on commits you haven't pushed, or on a branch nobody else has pulled. On a shared main branch you'll force everyone to reconcile.
---

[Luke Oliff.](/)

- [Blog](/blog)
- [CV](/cv)
- [Uses](/uses)
- [User manual](/user-manual)
- [Living with ADHD](/living-and-working-with-adhd)
- [RSS](/rss.xml)

[Luke Oliff · Writing](/blog)

# How to Split a Messy Git Commit Into Clean Ones

Aug 2026 · [Git](/blog/category/git) · 4 min read · [Luke Oliff](/authors/luke)

**TL;DR:** You don't fix a messy commit by force. Mark it edit in git rebase -i, run git reset HEAD^ to unstage everything, then rebuild it with git add -p, committing each logical change on its own.

You split a messy git commit by undoing it without losing the work, then rebuilding it in pieces. For the last commit that's `git reset HEAD^`. For an older one it's `git rebase -i`, mark the commit `edit`, and do the same reset there. Either way the real trick is `git add -p`, which stages changes one hunk at a time so each new commit says one thing.

This came back around after a [post on blog.gnoack.org](https://blog.gnoack.org/post/git-history-split) hit Hacker News in August 2026. Worth relearning. Most of us know the theory and still end up with a commit called "fixes" that touches nine files.

## Splitting a git commit that's still on top

If the mess is your latest commit, you don't need rebase at all.

```bash
git reset HEAD^
```

That's a mixed reset. It moves the branch back one commit and leaves every change sitting in your working tree, unstaged. The commit is gone, the code is not. Now rebuild:

```bash
git add -p
```

Git walks each hunk and asks `Stage this hunk [y,n,q,a,d,s,e,?]?`. Say `y` to the hunks that belong together, `n` to the rest. If a hunk is too coarse, `s` splits it into smaller ones, and `e` lets you edit the hunk by hand when even that isn't fine enough. Stage one logical change, commit it, repeat.

```bash
git commit -m "extract the retry helper"
git add -p
git commit -m "fix the off-by-one in the pager"
```

Keep going until `git status` is clean. One tangled commit is now three that each read on their own.

## Splitting a commit buried in history

The commit you regret is rarely the last one. Say it's three back. Start an interactive rebase from its parent:

```bash
git rebase -i HEAD~3
```

An editor opens with a list of commits, `pick` on each. Change `pick` to `edit` (or just `e`) on the one you want to break up, save, and close. Git replays up to that commit and stops, leaving it applied and staged. Undo it in place:

```bash
git reset HEAD^
```

Now you're in the same spot as before: the changes are unstaged, the commit is gone. Use `git add -p` and `git commit` as many times as you need. When you're done:

```bash
git rebase --continue
```

Git replays the commits that sat on top. If it can't, it stops for a conflict, you fix it, `git add` the file, and continue. The commits after the one you split keep their messages and their order.

One caveat that bites people: this rewrites history from that commit forward, so every hash downstream changes. Fine on a branch only you have touched. Not fine on `main` that ten people have pulled. Rebase before you push, not after.

## When you just want to leave something out of a commit

Sometimes you don't need to split an old commit, you just don't want to commit everything you changed right now. Same tool, different entry point:

```bash
git commit --patch
```

`--patch` (or `-p`) runs the hunk-by-hunk picker straight into the commit, skipping the staging step entirely. You get asked about each change and only the ones you approve go in. The rest stay in your tree for the next commit. I reach for this more than the rebase version, honestly, because catching the mess before it lands beats untangling it later.

The whole point of small commits is the review and the bisect later. A reviewer can reason about "fix the off-by-one" in ten seconds. Nobody can reason about "fixes" that also renamed a module and bumped a dependency. `git bisect` can't either. Splitting a git commit is you paying that forward: a few minutes now against a confused afternoon in six months when something breaks and the history is the only witness you've got.

[← Newer: Why I Trust Open Models More Than the Frontier Labs Now](/trust-open-models)

[All posts](/blog) →

## Search this site

`GET https://lukeocodes.dev/search.json?q=<words>` returns matching posts as JSON, each with its page URL and its markdown URL. Add `&limit=<1-50>` for more than 10. How it works: [/blog/search](https://lukeocodes.dev/blog/search.md).

```sh
curl "https://lukeocodes.dev/search.json?q=developer+relations"
```

---

© 2026 Luke Oliff. Posts live at the root of this site.

- [GitHub](https://github.com/lukeocodes)
- [LinkedIn](https://www.linkedin.com/in/lukeocodes)
- [X](https://x.com/lukeocodes)
- [Bluesky](https://bsky.app/profile/lukeocodes.dev)
- [Email](mailto:luke@lukeoliff.com)
- [RSS](/rss.xml)

## Cookies

This site sets one analytics cookie, from Google Analytics, and only in a browser that accepts the cookie banner. A client that does not run scripts sets no cookies, so there is nothing to accept or decline here.

[Cookie policy](/cookies)
