Rewriting Commit History with Interactive Rebase
Scenario
Modifying or Reordering Commits
In this example, let's assume you are working on a project and have made several commits. However, you realise that there is a mistake in one of the earlier commits, and you want to correct it. Additionally, you would like to reorder a few commits to improve the logical flow of the project's history.
Step 1: Starting the Interactive Rebase
To begin the interactive rebase, open a terminal or Git Bash and navigate to the root directory of your Git repository. Execute the following command:
Replace n
with the number of commits you want to include in the interactive rebase. For example, to include the last 5 commits, use git rebase -i HEAD~5
.
Step 2: Interactive Rebase Editor
After executing the command, Git will open an interactive rebase editor with a list of commits. Each commit is prefixed with the word "pick." This is where you can modify, reorder, or squash commits.
Example Interactive Rebase Editor:
Step 3: Modifying Commits
To modify a commit, replace the word "pick" with "edit" in front of the commit you wish to modify. For instance, if you want to modify "Commit 1: Fix typo in README," change it to:
Save and close the editor.
Step 4: Modifying the Selected Commit
Git will now pause the rebase process and allow you to modify the selected commit. Make the necessary changes using your preferred code editor. Once you are done, stage the changes by executing:
Then, amend the commit using the following command:
Update the commit message if needed and save the changes.
Step 5: Continuing the Rebase Process
After amending the commit, continue the rebase process by executing the following command:
Git will apply the changes made in the amended commit and proceed with the remaining commits.
Step 6: Reordering Commits
To reorder commits, open the interactive rebase editor again by executing:
Replace n
with the number of commits you included in the initial interactive rebase.
Within the interactive rebase editor, simply rearrange the order of the commits by moving their respective lines. Save and close the editor.
Step 7: Squashing Commits
Git's interactive rebase feature also allows you to squash commits together to create a more concise and coherent commit history.
To squash commits, open the interactive rebase editor once again:
Within the editor, change the word "pick" to "squash" or "s" for the commits you want to squash.
Example Interactive Rebase Editor with Squashing:
Save and close the editor.
Step 8: Addressing Considerations and Best Practices
When rewriting commit history, it is essential to keep in mind a few considerations and best practices:
Collaboration: Avoid rewriting commits that have been pushed to a shared repository. Rewriting history can cause conflicts for other team members who have based their work on the original commits.
Documentation: Ensure that the rewritten commits maintain a clear and meaningful history, making it easier for future contributors to understand the project's evolution.
Backup: Before performing any significant rewrite, create a backup or branch to preserve the original commit history in case anything goes wrong during the rebase process.
Communicate: If you are working on a shared repository, communicate with your team before rewriting commit history to ensure everyone is aware of the changes.
Last updated