Table of Contents
Keeping a clean and organized commit history is essential in any Git project. The git rebase command is a powerful tool that allows you to restructure your commit history, making it easier to understand and navigate. In this article, we will explore how to use git rebase to manage your commit history and maintain a tidy project timeline.
What is git rebase?
Git rebase is a command that lets you move or combine commits in a branch. It helps you maintain a linear and organized commit history by rewriting the commit sequence. Unlike merging, which combines branches while preserving their history, rebasing moves your branch to a new base commit, applying your changes on top of it.

When to use git rebase
Using git rebase is beneficial in several scenarios:
- Keeping a linear commit history: Rebasing allows you to avoid unnecessary merge commits, resulting in a more straightforward and linear history.
- Combining multiple commits: You can use
git rebaseto combine multiple small commits into a single, meaningful commit. - Integrating changes: Before merging a feature branch into the main branch, you can use
git rebaseto integrate changes from the main branch into your feature branch.
Basic git rebase
The most basic use of git rebase is to move your current branch to a new base commit. Suppose you have a feature branch that you want to rebase onto the latest changes in the main branch.
Example command
git checkout feature-branch
git rebase mainIn this example, Git moves the feature-branch to the tip of the main branch, applying its commits on top of the latest changes in main. This process avoids creating a merge commit, resulting in a cleaner history.
Interactive rebasing with git rebase
Interactive rebasing is a powerful feature of git rebase that allows you to modify commits before finalizing the rebase. You can edit, reorder, squash, or drop commits during the interactive rebase process.
Starting an interactive rebase
To start an interactive rebase, use the -i or --interactive flag followed by the commit you want to rebase onto. For example, to interactively rebase the last three commits:
Example command
git rebase -i HEAD~3This command opens an editor with a list of the last three commits. Each line represents a commit and contains a command (e.g., pick), followed by the commit hash and message.
Editing commits during an interactive rebase
During the interactive rebase, you can use different commands to modify the commits:
- pick: Use the commit as-is.
- edit: Stop to edit the commit.
- squash: Combine this commit with the previous one.
- reword: Change the commit message.
To squash the last two commits into a single commit, change the command for the second commit from pick to squash.
Example interactive rebase
pick abc1234 Add new feature
squash def5678 Fix typo in new featureAfter saving and closing the editor, Git pauses the rebase and prompts you to edit the commit message for the squashed commit.
Completing the rebase
After making the desired changes during the interactive rebase, save and close the editor. Git continues the rebase process, applying the changes. If there are conflicts, Git pauses and prompts you to resolve them.
Example command to continue rebase
git rebase --continueUse this command to resume the rebase after resolving conflicts or making changes. If you want to abort the rebase and return to the previous state, use:
Example command to abort rebase
git rebase --abortAdvantages of using git rebase
Git rebase offers several advantages, especially when maintaining a clean commit history:
1. Linear commit history
Rebasing helps avoid unnecessary merge commits, resulting in a linear and easy-to-read history. This makes it easier to trace changes and understand the project’s evolution.
2. Combining related commits
With interactive rebasing, you can squash multiple related commits into a single, more meaningful commit. This helps reduce clutter in the history and makes it easier for others to review the changes.
3. Streamlined integration
By rebasing a feature branch onto the latest changes in the main branch, you can ensure that your branch is up to date without creating extra merge commits.
Best practices for using git rebase
While git rebase is a powerful tool, it should be used carefully to avoid disrupting the shared history. Here are some best practices for using git rebase:
1. Avoid rebasing public branches
Never rebase branches that have been pushed to a shared repository and are used by others. Rebasing changes the commit history, which can lead to confusion and conflicts for collaborators.
2. Use rebase for local changes
Rebase is best suited for modifying local changes that have not yet been shared. This allows you to tidy up your commit history before pushing it to the shared repository.
3. Resolve conflicts carefully
When rebasing, conflicts may arise if there are overlapping changes. Carefully review and resolve conflicts to ensure that your changes are correctly integrated.
Example workflow using git rebase
Let’s go through a typical workflow using git rebase to keep a feature branch up to date with the main branch:
- Switch to the feature branch:
git checkout feature-branch- Rebase onto the main branch:
git rebase main- Resolve conflicts if any:
git rebase --continue- Push the rebased branch:
git push -fThis workflow keeps your feature branch up to date with the latest changes in the main branch without creating additional merge commits.
Conclusion
Git rebase is an essential tool for developers looking to maintain a clean and organized commit history. By using git rebase, you can move, combine, and edit commits to create a linear and meaningful project history. Remember to use rebase responsibly, especially when working with shared branches. By following best practices, you can leverage the power of git rebase to streamline your development workflow and keep your project’s history tidy and easy to navigate.