Imagine you’re working on a software project with a version control system like Git, and there are two branches: master and feature. The master branch represents the stable version of the software, while the feature branch contains new features that are still under development.
As you work on the feature branch, other developers might be making changes to the master branch to fix bugs or add other features. In the meantime, your feature branch has progressed with several commits of its own.
Now, you reach a point where your feature branch is ready to be merged into the master branch. However, you want to ensure that your new features are built on top of the most recent changes in the master branch. This is where git rebase comes into play.
When you perform a rebase, you’re essentially “replaying” the commits from your feature branch on top of the latest commits from the master branch. This process creates new, separate copies of your commits and places them at the tip of the master branch.
git rebase Command Examples
1. Rebase the current branch on top of another specified branch:
# git rebase new_base_branch
2. Start an interactive rebase, which allows the commits to be reordered, omitted, combined or modified:
# git rebase -i target_base_branch_or_commit_hash
3. Continue a rebase that was interrupted by a merge failure, after editing conflicting files:
# git rebase --continue
4. Continue a rebase that was paused due to merge conflicts, by skipping the conflicted commit:
# git rebase --skip
5. Abort a rebase in progress (e.g. if it is interrupted by a merge conflict):
# git rebase --abort
6. Move part of the current branch onto a new base, providing the old base to start from:
# git rebase --onto new_base old_base
7. Reapply the last 5 commits in-place, stopping to allow them to be reordered, omitted, combined or modified:
# git rebase -i HEAD~5
8. Auto-resolve any conflicts by favoring the working branch version (theirs keyword has reversed meaning in this case):
# git rebase -X theirs branch_name