Skip to content

Interactive Rebase to Clean History

Medium ~35 min

What you'll build

A feature branch with 5 messy commits rebased into 2 clean, well-named commits using interactive rebase.

Goals

  • Squash multiple commits into one
  • Reword commit messages during rebase
  • Reorder commits by editing the todo list
  • Resolve conflicts that arise during rebase

Prerequisites

  • Basic Git commits and branches
  • Merging concepts
  • Command line comfort

Steps

  1. 1

    Set up a messy branch

    Create a feature branch and make 5 small commits with messages like 'wip', 'fix typo', 'add tests', 'wip2', 'final'. This simulates real messy development.

    Code
    git checkout -b feature/cleanup
    # Make 5 commits with messy messages
    echo 'a' >> file.txt && git add . && git commit -m 'wip'
    echo 'b' >> file.txt && git add . && git commit -m 'fix typo'
    echo 'c' >> file.txt && git add . && git commit -m 'add tests'
    echo 'd' >> file.txt && git add . && git commit -m 'wip2'
    echo 'e' >> file.txt && git add . && git commit -m 'final'

    Expected result

    A feature branch with 5 commits ahead of main.

  2. 2

    Start an interactive rebase

    Run git rebase -i HEAD~5 to open the last 5 commits in your editor. Git shows a 'todo list' with one line per commit and an action verb (pick, squash, reword, etc.).

    Code
    git rebase -i HEAD~5

    Expected result

    Your editor opens showing 5 lines starting with 'pick <hash> <message>'.

  3. 3

    Squash commits together

    Change the action verb on lines 2, 3, and 4 from 'pick' to 'squash' (or 's'). This tells Git to combine those commits into the one above them. Save and close.

    Code
    # In the editor, change:
    # pick abc1234 wip
    # pick def5678 fix typo
    # pick ghi9012 add tests
    # pick jkl3764 wip2
    # pick mno5321 final
    #
    # To:
    pick abc1234 wip2
    squash def5678 wip
    squash ghi9012 fix typo
    pick jkl3764 add tests
    squash mno5321 final

    Expected result

    Git combines the squashed commits and prompts for a new commit message.

  4. 4

    Reword the squashed messages

    When Git prompts for the combined commit message, delete the old messages and write a clean one like 'Add core feature with tests'. Repeat for the second squashed group.

    Code
    # First squash prompt — replace all lines with:
    Add core feature implementation
    
    # Second squash prompt — replace with:
    Finalize with tests and cleanup

    Expected result

    Two clean commits replace the original five.

  5. 5

    Verify the clean history

    Run git log --oneline to see the result. You should see 2 commits with clean messages instead of 5 messy ones. Verify the file content is unchanged.

    Code
    git log --oneline -5
    # Expected:
    # <hash> Finalize with tests and cleanup
    # <hash> Add core feature implementation
    # ... (main's commits below)
    
    git diff main --stat  # Should show the same total changes

    Expected result

    Log shows 2 clean commits; diff against main is unchanged.

Try it yourself

What you learned

You mastered interactive rebase — the tool that lets teams keep clean, readable histories despite messy development. It's essential for preparing PRs.

Related Resources

Related Lessons

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic