Home / Git

Git Rebase: Interactive Rebasing and Squashing Commits

Git is a powerful version control tool that allows developers to track changes, manage collaboration, and maintain a clean and organized codebase. One of the key features of Git is the ability to rebase, which allows developers to combine, rearrange, and modify commits to create a more concise and meaningful commit history. In this article, we will explore one aspect of Git rebase - interactive rebasing and squashing commits.

What is Git Rebase?

Before diving into interactive rebasing and squashing commits, let's briefly review the concept of Git rebase. In Git, rebase is the process of applying changes from one branch to another. It is typically used to integrate changes from a feature branch into the main branch (e.g., master or main). Rebase allows you to maintain a linear commit history, which can make it easier to understand and navigate the codebase.

Interactive Rebasing

Interactive rebasing takes Git rebase a step further by providing fine-grained control over individual commits. It allows you to edit, reorder, combine, and even remove commits to create a cleaner and more logical commit history. Interactive rebasing is particularly useful when preparing a branch for a pull request or when cleaning up a messy commit history before merging.

To start an interactive rebase, you can use the following command:

git rebase -i <base_commit>

In the command above, <base_commit> is the commit that you want to start rebasing from (usually the parent commit of your current branch).

Once you run the command, Git will open an interactive editor with a list of commits. Each commit will have a corresponding action that can be performed on it. These actions include:

  • pick: keep the commit as is
  • edit: pause the rebase, allowing you to make changes to the commit
  • reword: change the commit message
  • squash: combine the commit with the previous commit and create a new commit
  • fixup: like squash, but discards the commit message
  • drop: remove the commit completely

By modifying the actions for each commit, you can control how the history is rewritten during the rebase process.

Squashing Commits

Squashing commits is a particularly powerful feature of interactive rebasing. It allows you to combine multiple commits into a single, cohesive commit. This can be useful when you have made a series of small, incremental changes that could be logically grouped together.

To squash commits during an interactive rebase, you need to mark the commits you want to squash with the squash action. When you save and close the interactive editor, Git will combine the marked commits into one, and you will be prompted to provide a new commit message.

Here's an example of how you can squash commits during an interactive rebase:

  1. Run the interactive rebase command: git rebase -i <base_commit>.
  2. In the interactive editor, change the pick action to squash or s for the commits you want to squash. Make sure to keep the pick action for the commit you want to keep as the final commit.
  3. Save and close the editor.
  4. Git will combine the marked commits into one and prompt you to provide a new commit message.
  5. Edit the commit message if needed, save, and close the editor.

By squashing commits, you can create a more concise and meaningful commit history. This can make it easier for other developers to understand and review your changes in the future.

Conclusion

Git rebase, especially when combined with interactive rebasing and commit squashing, is a powerful tool for managing and organizing your commits. It allows you to create a clean and logical commit history, making it easier to navigate and understand your codebase. By mastering interactive rebasing and squashing commits, you can improve collaboration and maintain a more professional and efficient workflow in your Git projects.


noob to master © copyleft