What is Rebase?
Git rebase is a command that allows you to move or combine a sequence of commits to a new base commit. Instead of merging branches and creating a merge commit, rebase rewrites the commit history by applying your changes on top of another branch, resulting in a cleaner, linear commit history.
Purpose
- Cleaner History: Creates a linear, more readable project history by eliminating unnecessary merge commits.
- Conflict Resolution: Allows for conflicts to be resolved at each commit rather than in a single merge commit.
Steps for Rebasing
- Checkout the Branch to Rebase:
- Command:
git checkout feature-branch
- Start the Rebase Process:
- Resolve Conflicts (if any):
- Manually fix the conflicts in each file.
- Stage the resolved changes.
- Command:
git add <file>
- Continue rebasing.
- Command:
git rebase --continue
- Force Push (if rebasing a public branch):
- Command:
git push --force
Example
- Rebasing
feature-branch
onto main
.
Benefits
- Linear History: Makes the project history easier to understand.
- Easier to Bisect: Helps in finding bugs by making
git bisect
more effective.
Considerations
- Rewrite History: Rebasing changes the commit history, so it’s advisable not to rebase shared branches.