Rewriting Commit History with Interactive Rebase

The interactive rebase feature in Git is a powerful tool that allows you to modify, reorder, or combine commits in your commit history.

This feature is particularly useful when you want to clean up your commit history before sharing it with others or when preparing a clean set of commits for integration into a main branch.

In this section, we will explore how to use interactive rebase and discuss best practices and considerations when rewriting commit history.

  1. What is Interactive Rebase? Interactive rebase is a feature in Git that enables you to selectively modify the commit history by interacting with each commit in a step-by-step manner. It allows you to alter commit messages, combine multiple commits, reorder commits, or even remove commits from your commit history.

  2. Using Interactive Rebase: To start an interactive rebase, you need to specify the commit from which you want to begin the rebase. Open your terminal and navigate to your Git repository. Then, execute the following command:

git rebase -i <commit>

Replace <commit> with the commit hash or a reference to the commit you want to start the interactive rebase from. For example, you can use a branch name, such as master, or a relative reference like HEAD~3 to indicate the third last commit.

  1. Interactive Rebase Commands: Once you start the interactive rebase, Git will open a text editor with a list of commits and corresponding commands. Here are some commonly used commands during interactive rebase:

  • pick: Keeps the commit as is.

  • reword: Allows you to change the commit message.

  • edit: Pauses the rebase process to let you modify the commit content.

  • squash: Combines the commit with the previous commit.

  • fixup: Combines the commit with the previous commit, discarding the commit message.

  • drop: Removes the commit from the commit history.

  1. Modifying Commit Messages: To modify a commit message during an interactive rebase, change the command from pick to reword for the corresponding commit in the text editor. Save the changes and exit the editor. Git will then prompt you to modify the commit message. Edit the message, save, and exit the editor again to continue the rebase.

  2. Reordering Commits: To reorder commits, simply rearrange the lines representing the commits in the text editor. Git will apply the commits in the order they appear in the list.

  3. Combining Commits: You can combine multiple commits into a single commit using the squash or fixup commands. The squash command preserves the commit message, while the fixup command discards it. To combine commits, change the command of the commits you want to combine to squash or fixup in the text editor. Git will prompt you to modify the resulting commit message or discard it, respectively.

  4. Removing Commits: If you want to remove a commit from the commit history, simply delete the corresponding line from the text editor during the interactive rebase. Git will exclude the deleted commit from the final commit history.

  5. Best Practices and Considerations: When rewriting commit history with interactive rebase, keep the following best practices and considerations in mind:

  • Interactive rebase is best suited for local branches, as rewriting history on shared branches can cause issues for collaborators.

  • Be cautious when rewriting commits that have already been pushed to a shared repository. This can create conflicts for other team members.

  • Use interactive rebase primarily for cleaning up and organising your commits, rather than modifying important or critical parts of the commit history.

  • Keep your changes focused and logically grouped. Combining too many unrelated changes into a single commit can make it harder to understand the commit history.

Interactive rebase provides a flexible and powerful way to rewrite commit history in Git.

Last updated