Last Updated: January 3, 2026
Reordering commits can be a powerful way to refine your project history. Whether you want to group similar changes together, highlight significant work, or apply a specific sequence for clarity, understanding how to effectively reorder commits is essential.
As developers, we often find ourselves needing to make our commit history tell the right story, and this chapter will guide you through the process of reordering commits using Git.
Before diving into reordering, let's clarify how Git manages commits. Each commit in Git is a snapshot of your project at a particular point in time. Commits form a directed acyclic graph (DAG), where each commit points back to its parent(s). This structure defines the order of commits and enforces a chronological flow.
When you reorder commits, you are altering this structure. You need to be mindful of how these changes can affect the commit history, especially with regard to parent-child relationships. Understanding this helps prevent confusion in collaborative environments where multiple branches may be involved.
Here's a simplified representation of a commit history:
In this example, commit D is the latest commit, and it is based on C, which is based on B, and so forth.
Before you start reordering commits, it's crucial to prepare your working directory. Make sure you have a clean working state and that your changes are committed. An uncommitted working directory can lead to conflicts during the reordering process.
To check the status of your working directory, you can use:
If you have uncommitted changes, consider committing them or stashing them using:
This ensures you won't lose any work during the reordering process.
The most effective way to reorder commits in Git is through interactive rebase. This operation allows you to manipulate your commit history directly. To start an interactive rebase, you specify the number of commits you want to go back from the current HEAD. For example, if you want to reorder the last four commits, you will run:
This command opens your default text editor with a list of the last four commits. The format will look something like this:
To reorder the commits, simply change the order of the lines. For instance, if you want to move "Commit message 3" to the top, modify it as follows:
After saving and closing the editor, Git will attempt to apply the commits in the new order.
While reordering commits, conflicts may arise, especially if changes in the reordered commits overlap. If a conflict occurs, Git will pause the rebase process and allow you to resolve the conflict manually.
You'll see output indicating that you have conflicts to resolve:
To fix the conflicts:
Continue the rebase process with:
If you encounter additional conflicts, repeat the process. If at any point you feel overwhelmed or make a mistake, you can abort the rebase:
This command will restore your branch to its state before the rebase began.
Reordering commits can improve the readability and organization of your commit history. Here are a few common scenarios where reordering might be beneficial:
Once you have completed the reordering process, it's essential to verify the integrity of your project. Run your tests or check the functionality of your application to ensure that everything is still working as expected. This step is crucial, especially in collaborative environments where your changes may affect others.
To review the log after reordering, you can use:
This command will give you a visual representation of your commit history, making it easier to see the changes you've made.
Now that you understand how to reorder commits and the implications of doing so, you are ready to explore how to edit commits for further refinement.
In the next chapter, we will look at the process of editing commit messages and content, allowing you to enhance your commit history even more.