Mastering Git Rebase (2023): Workflow, Options & Best Practices
A step-by-step tutorial on using git rebase, highlights interesting options to enhance your productivity, and shares essential best practices to maintain a clean commit history and minimize conflicts.
TL;DR
Use git rebase BRANCH_NAME
to pull changes from the branch BRANCH_NAME while keeping your current changes.
Written by @bazamel_
The “git rebase” command integrates changes from one branch onto another by applying each individual commit in chronological order.
How do I use git rebase?
To use the git rebase
command, follow the steps below:
- Checkout the branch - Start by checking out the branch you want to rebase onto, for example,
main
:git checkout main
. - Initiate the rebase - Run the rebase command, specifying the branch you want to integrate, such as
feature-branch
:git rebase feature-branch
. - Resolve conflicts - If there are any conflicts during the rebase process, Git will pause and prompt you to resolve them manually.
- Continue the rebase - After resolving conflicts, use
git rebase --continue
to continue applying the remaining commits. - Push the changes - Once the rebase is complete, push the rebased branch to the remote repository:
git push origin main
.
By performing these steps, the changes from feature-branch
are integrated into main
while maintaining a clean and linear commit history.
Why is rebasing useful?
Using git rebase
in these scenarios allows for better collaboration, easier conflict resolution, and a cleaner commit history, leading to more efficient development workflows:
-
Feature branch integration - Rebase allows you to integrate changes from a feature branch onto the main branch, providing a clean and linear commit history, making it easier to understand the project’s development timeline.
-
Updating a forked repository - When working with a forked repository, using
git rebase
helps sync your fork with the original repository, ensuring a smoother and up-to-date integration of changes. -
Interactive rebase - By using an interactive rebase (
git rebase -i
), you can rearrange, edit, combine, or delete commits, enabling you to create cleaner and more organized commit histories before merging or pushing your changes. -
Squashing commits - Rebase allows you to combine multiple commits into a single commit (squashing), reducing the overall number of commits and making the history more concise and focused.
-
Resolving conflicts - During a rebase, if conflicts arise between your branch and the target branch, you can resolve them interactively, ensuring that the merged code is conflict-free and functions as intended.
-
Maintaining a clean history - By regularly rebasing your branch onto the latest changes from the main branch, you can keep your branch up to date and avoid unnecessary merge commits, resulting in a cleaner and more understandable commit history.
-
Pull request preparation - When preparing a pull request, rebasing your branch onto the target branch helps ensure a smooth and fast merging process, as it minimizes the chances of conflicts and keeps the commit history coherent.
Some options worth remembering
-i
or--interactive
- Enables interactive mode, allowing you to modify, rearrange, or squash commits during the rebase process.
Example: git rebase -i HEAD~3
--continue
- Resumes the rebase process after resolving conflicts or making changes to the code.
Example: git rebase --continue
--abort
- Aborts the ongoing rebase operation and restores the branch to its original state before the rebase started.
Example: git rebase --abort
--skip
- Skips the current commit if you encounter a conflict during the rebase, proceeding to the next commit.
Example: git rebase --skip
--onto <target>
- Specifies a new base or target branch onto which the current branch should be rebased.
Example: git rebase --onto main feature-branch
--autosquash
- Automatically applies any commits marked with the prefix “squash!” or “fixup!” in the interactive rebase, without further interaction.
Example: git rebase -i --autosquash HEAD~5
--ignore-whitespace
- Ignores whitespace changes when comparing commits, useful when conflicts arise due to whitespace differences.
Example: git rebase --ignore-whitespace
Best practices for using git rebase
-
Rebase local feature branches - It keeps your local feature branches up to date with the latest changes from the main branch, reducing conflicts and facilitating smoother integration.
-
Rebase before sharing code - It ensures that the code you share with others is based on the latest changes, preventing unnecessary merge conflicts and making collaboration easier.
-
Avoid rebasing public branches - It helps maintain a stable and consistent history for public branches (such as
main
), as rebasing can modify commit history and cause issues for other developers. -
Rebase only on clean branches - It reduces the chances of conflicts and ensures that the rebase applies cleanly, saving time spent on conflict resolution.
-
Resolve conflicts promptly - It prevents conflicts from snowballing and becoming more challenging to resolve as the rebase progresses.
-
Use interactive rebase for commit organization - It allows you to squash or reword commits, creating a clean and coherent commit history that tells a meaningful story.
-
Backup or branch protection - Before performing a rebase, create a backup or enable branch protection mechanisms like protected branches or branch permissions to avoid accidental data loss.
-
Communicate with team members - Inform your team members when you plan to rebase, especially if you are working on shared branches, to ensure everyone is aware and can adapt their workflow accordingly.
-
Test thoroughly after a rebase - Ensure that the rebased code functions as expected by running tests and performing quality assurance checks before merging or pushing the changes.