Chuck's Academy

GIT

Rebase and Squash in Git

In this chapter, we will learn two advanced Git techniques that will help you maintain a clean and organized commit history: rebase and squash. These tools are useful for reordering commits, merging multiple commits into one, and avoiding unnecessary merges. By the end of this chapter, you will know how to use rebase safely and how to combine commits to improve history clarity.

What is Rebase?

The git rebase command allows you to move or apply commits from one branch onto another. Unlike git merge, which creates a new merge commit, git rebase takes the commits from one branch and "reapplies" them on top of another branch. This keeps the history more linear and easier to follow.

Basic Use of Rebase

Suppose you have been working on a branch called feature-X and now wish to integrate those changes into the main branch. Instead of merging, you can rebase to apply the commits from feature-X onto main.

First, switch to the feature-X branch:

bash

Then, perform the rebase onto main:

bash
"With the command 'git rebase main', you apply the commits from the feature-X branch on top of the main branch, keeping a cleaner history without a merge commit."

If conflicts arise during the rebase, Git will ask you to resolve them manually. After resolving the conflicts, use:

bash
"The 'git rebase --continue' command allows you to continue the rebase process after resolving conflicts."

Interactive Rebase

The interactive rebase offers even more control over the commits. You can use this mode to reorder, modify, or delete commits before merging them into another branch. To start an interactive rebase, use the following command:

bash
"The command 'git rebase -i followed by the commit identifier' opens an editor where you can choose what to do with each commit: reorder, merge, or modify."

During the interactive rebase, you can choose different options for each commit:

  • pick: Leave the commit as is.
  • reword: Change the commit message.
  • squash: Merge the commit with the previous one.
  • drop: Delete the commit.

Squash: Merging Commits

The squash is a technique that allows you to combine multiple commits into a single one. This is useful when you make several small commits and want to merge them to keep a cleaner commit history. Squash is commonly used in an interactive rebase.

For example, if you have a series of commits like:

  • Fix typo
  • Add feature
  • Fix bug

You can merge them into a more descriptive single commit using the interactive rebase:

bash
"With the command 'git rebase -i HEAD tilde 3', you start an interactive rebase for the last three commits. There you can use squash to merge them into one."

During the interactive rebase, select the squash option for the commits you want to merge. Git will prompt you to edit the message of the resulting commit.

Precautions When Using Rebase

While rebase is very useful, you must be careful when using it on branches that have already been shared with others. Rewriting the history with rebase on such branches can cause issues since re-applied commits will have new identifiers. To avoid this, it is recommended to use rebase only on local branches that have not been shared.

If you have performed a rebase on a branch that has already been shared and you need to force the update in the remote repository, you can use:

bash
"The 'git push --force' command forces the update of the history in the remote repository after a rebase, but you must be careful as this can overwrite others' work."

Conclusion

In this chapter, we have learned how to use git rebase to maintain a clean and organized commit history, as well as the squash technique to combine several commits into one. Although these tools are powerful, it's important to use them with caution, especially when collaborating with other developers. In the next chapter, we will explore how to use stash and clean your repository to maintain an organized work environment.


Ask me anything