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
If conflicts arise during the rebase, Git will ask you to resolve them manually. After resolving the conflicts, use:
bash
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
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
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
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.
- Introduction to Git and Version Control
- Installation of Git and Initial Setup
- Understanding Repositories
- Basic Workflow in Git
- Working with Branches in Git
- Collaborating with Other Developers
- Undoing Changes in Git
- Working with Tags in Git
- Rebase and Squash in Git
- Stashing and Cleaning in Git
- Advanced Git Commands
- Hooks and Automation in Git
- GitHub and Repository Management
- Best Practices in Git
- Conclusion and Final Tips