Git Rebase onto
How to modify the commit history with git rebase onto
Found a typo? Edit meWhen you are working with Git, you may find yourself in a situation where you need to rebase your branch onto another branch.
This can happen when you want to incorporate changes from one branch into another, but you want to keep the commit history clean and linear.
What is git rebase --onto
?
The git rebase --onto
is a powerful command when you need more control over the rebase process.
It allows you to specify a different branch or commit to rebase your changes onto, which is not necessarily the upstream branch of the current branch.
The command takes three arguments:
- The new base commit
- The exclusive starting commit of the range to rebase
- The inclusive ending commit of the range to rebase
Syntax
When to use --onto
?
1. Rebasing a Subset of Commits
When you want to rebase a subset of commits from your branch onto another branch:
)
)
If you want to rebase A
into Z
, you can use the following command:
()
()
This command will rebase the commits from feature
branch onto the master
branch, starting from commit Z
.
2. Skipping Commits
When you want to skip a set of commits from your current branch:
)
)
If you want to skip A
and B
and rebase the rest onto Z
, you can use the following command:
()
This skips commits A
and B
and applies the remaining commits onto the master
branch.
)
)
3. Rebasing to a different base
When you want to rebase a branch onto a completely different base that is not its current base.
\
)
)
)
If you want to rebase feature2
onto feature1
:
()
This command will rebase feature2
onto feature1
.
)
)