Skip to content
vast-cow's blog
Go back

How to Squash Past Commits with Git Rebase

Edit page

Squashing commits with Git’s rebase command is useful when you want to clean up your commit history. Here’s a step-by-step guide.


1. Check Which Commits to Squash

First, review the commit log to decide which commits you want to combine:

git log --oneline

Example:

a1b2c3d Fix typo
d4e5f6g Add feature X
h7i8j9k WIP: implement feature X
...

Suppose you want to squash Add feature X and WIP: implement feature X.


2. Start an Interactive Rebase

Run git rebase -i using the commit before the ones you want to squash as the base:

git rebase -i <base_commit_id>

In this case, if a1b2c3d is the commit before, you would run:

git rebase -i a1b2c3d

3. Edit the Commit List

Your editor will open with something like this:

pick d4e5f6g Add feature X
pick h7i8j9k WIP: implement feature X

Change the second pick to squash (or just s):

pick d4e5f6g Add feature X
squash h7i8j9k WIP: implement feature X

4. Edit the Commit Message

Next, Git will prompt you to combine commit messages:

# This is a combination of 2 commits.
# The first commit’s message is:
Add feature X

# The following commit message will also be included:
WIP: implement feature X

Clean it up so the final message is concise:

Add feature X

5. Finish the Rebase

Save and close the editor. Git will apply the rebase and squash the commits into one.


6. Troubleshooting


7. Squashing Including the Root Commit (--root)

By default git rebase -i <base> rebases starting after the specified base commit. If you want to include the very first (root) commit in an interactive rebase — for example to edit or squash the initial commit together with later commits — use the --root option:

git rebase -i --root

What this does

Common uses & examples

  1. Squash all commits into a single initial commit If you want a single commit that contains the whole history, keep the root as pick and mark every other commit as squash (or s):

    pick a1b2c3d Initial commit
    squash d4e5f6g Add feature X
    squash h7i8j9k WIP: implement feature X
    ...

    After saving, Git will open the combined commit-message editor so you can craft the final message.

  2. Edit or reword the root commit message Change the first line to reword (or r) to update the initial commit message during the rebase:

    reword a1b2c3d Initial commit
    pick d4e5f6g Add feature X
    ...

    The rebase will stop and prompt you to edit the root commit message.

  3. Amend the root content Use edit on the root entry if you need to change the actual tree of the root commit (add/remove files). Rebase will stop at that commit; then you can modify files, git add them, and run git commit --amend before continuing the rebase:

    # interactive todo:
    edit a1b2c3d Initial commit
    pick d4e5f6g Add feature X
    ...
    # when rebase stops:
    # make changes to the working tree
    git add <files>
    git commit --amend --no-edit   # or edit message as needed
    git rebase --continue

Caveats


Summary


Edit page
Share this post:

Comments


Previous Post
Unlocking GNOME Keyring on a Remote Machine via X11 Forwarding
Next Post
How to Safely Manually Edit the Bash History File