Git Rebase Squash Order Explained Simply | Generated by AI

Home 2025.09

When you run git rebase -i (interactive rebase) to squash commits, Git follows a very specific order and logic:


1. Default Order

When you start an interactive rebase (e.g., git rebase -i HEAD~5), Git opens your editor with the commits listed from oldest to newest (top = oldest, bottom = newest). The order matters: this is the sequence Git will try to replay onto the new base.


2. Pick vs. Squash

So if you write:

pick  a1b2c3  Initial feature
squash d4e5f6  Minor fix
squash g7h8i9  Small cleanup
pick  j1k2l3  Another feature

Git will merge d4e5f6 and g7h8i9 into a1b2c3, producing a single commit.


3. Rearranging Commits

You can reorder commits directly in the rebase editor by moving lines up or down. For example:

pick  a1b2c3  Initial feature
pick  j1k2l3  Another feature
squash d4e5f6  Minor fix
squash g7h8i9  Small cleanup

Here, Git will first apply a1b2c3, then j1k2l3. The squash commits (d4e5f6 and g7h8i9) will now squash into j1k2l3, since squash always merges into the most recent preceding pick.


4. Editing Messages

After squashing, Git pauses and lets you edit the commit message for the squashed commit. You can:


5. Common Pitfalls


✅ In short:


Do you want me to illustrate this with a before/after commit history example (showing git log --oneline before and after squashing)? That usually makes the order clearer.


Back Donate