Course contentsShow
← Git & GitHub
Lesson 83 of 965. Advanced Productivity and WorkflowFree lesson

Reordering Commits to Improve Logical Flow

Rearrange the sequence of commits to ensure that the project history follows a logical, understandable progression of features.

In this lesson, you will learn how to rearrange the sequence of your commits to ensure your project history tells a clear, logical story.

As you develop, your commit history often becomes a "brain dump" of your workflow. You might fix a bug, then start a feature, realize you need a configuration change for that feature, and commit it out of order. While the code works, a reviewer looking at your history will be confused by the jumping back and forth. Reordering commits allows you to group related changes together, making your project history readable and professional.

How to Reorder with Interactive Rebase

You can reorder commits using git rebase -i. Suppose your current history looks like this:

pick a1b2c3d Add feature login
pick e4f5g6h Fix typo in README
pick i7j8k9l Add database config for login

The "database config" commit logically belongs with "Add feature login." To fix this, run git rebase -i HEAD~3. Your text editor will open with a list of commits. Simply drag and drop the lines into your desired order:

pick a1b2c3d Add feature login
pick i7j8k9l Add database config for login
pick e4f5g6h Fix typo in README

When you save and close the editor, Git will pause to replay the commits in the new order. If there are no conflicts, your branch is updated.

Why This Matters

When you submit a Pull Request, maintainers often review commits one by one. If your commits are in a logical sequence—where setup or dependencies happen before the features that rely on them—the reviewer can follow your thought process easily. This leads to faster approvals and fewer questions about your implementation.

Just remember: never reorder commits that have already been pushed to a shared public branch, as this alters the commit hashes and can disrupt your teammates’ work. Only reorder commits that exist strictly on your local feature branch before you merge or share them.

Key Takeaway: Reordering commits through interactive rebase allows you to curate your history into a coherent narrative, making your code changes easier to review and maintain.