0
0
Gitdevops~7 mins

Reordering commits in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to change the order of your recent work steps in Git to make your history cleaner or fix mistakes. Reordering commits lets you rearrange these steps before sharing your work with others.
When you want to group related changes together before pushing to a shared repository
When you accidentally committed changes in the wrong order and want to fix it
When preparing a pull request and want a logical sequence of commits
When cleaning up your commit history to make it easier for others to review
When you want to squash or reorder commits to simplify the project history
Commands
Shows the last 5 commits in short form so you can see their order and decide how to reorder them.
Terminal
git log --oneline -5
Expected OutputExpected
a1b2c3d Fix typo in README b2c3d4e Add user login feature c3d4e5f Update styles for homepage d4e5f6a Improve error handling e5f6a7b Initial commit
--oneline - Shows each commit in one line for easy reading
-5 - Limits output to last 5 commits
Starts an interactive rebase for the last 4 commits so you can reorder them. This opens a text editor with the commits listed.
Terminal
git rebase -i HEAD~4
Expected OutputExpected
Successfully rebased and updated refs/heads/main.
-i - Interactive mode to edit commit order and messages
HEAD~4 - Specifies the last 4 commits to include in the rebase
Checks the new order of commits after reordering to confirm the changes.
Terminal
git log --oneline -5
Expected OutputExpected
c3d4e5f Update styles for homepage b2c3d4e Add user login feature a1b2c3d Fix typo in README d4e5f6a Improve error handling e5f6a7b Initial commit
--oneline - Shows commits in one line
-5 - Limits output to last 5 commits
Key Concept

If you remember nothing else from this pattern, remember: interactive rebase lets you safely reorder recent commits before sharing your work.

Common Mistakes
Running 'git rebase -i' with a number larger than the number of commits available
Git will show an error because it cannot find that many commits to reorder.
Check the number of commits with 'git log --oneline' and use a correct count with 'git rebase -i HEAD~N'.
Editing the rebase todo list incorrectly, such as deleting lines or using wrong commands
This can cause the rebase to fail or produce unexpected commit history.
Only reorder lines by moving them up or down, or use supported commands like 'pick', 'reword', 'squash'. Save and exit properly.
Trying to reorder commits that have already been pushed to a shared repository
Reordering published commits rewrites history and can cause conflicts for others.
Only reorder local commits that have not been pushed, or coordinate with your team before rewriting shared history.
Summary
Use 'git log --oneline -N' to view recent commits and decide which to reorder.
Run 'git rebase -i HEAD~N' to start interactive rebase for the last N commits.
Rearrange commits by moving lines in the editor, then save to apply the new order.
Verify the new commit order with 'git log --oneline -N'.