These days, developers spend a lot of time reviewing merge requests
and taking these reviews into account to improve the code. We'll discuss how
Git rebase can help in
speeding up these review cycles. But first, let's take a look at some
workflow considerations.
Different ways to rework a merge request
A developer who worked on some code changes and created a merge
request with these changes will often have to rework them. Why does
this happen? Tests can fail, bugs are found, or reviewers suggest
improvements and find shortcomings.
Simple but messy method: add more commits
One way to rework the code changes is to make more changes in some new
commits on top of the branch that was used to create the merge
request, and then push the branch again to update the merge
request.
When a number of commits have been added in this way, the merge
request becomes problematic:
- It's difficult to review by looking at all the changes together.
- It's difficult to review the commits separately as they may contain different unrelated changes, or even multiple reworks of the same code.
Reviewers find it easier to review changes split into a number of small,
self-contained commits that can be reviewed individually.
Pro method: rebase!
A better method to prepare or rework a merge request is to always
ensure that each commit contains small, self-contained, easy-to-review
changes.
This means that all the commits in the branch may need reworking
instead of stacking on yet more commits. This approach might seem much
more complex and tedious, but git rebase
comes to the rescue!
Rework your commits with git rebase
If your goal is to build a merge request from a series of small,
self-contained commits, your branch may need significant rework before its
commits are good enough. When the commits are ready, you can push the branch
and update or create a merge request with this branch.
Start an interactive rebase
If your branch is based on main
, the command to rework your branch
is:
git rebase -i main
I encourage you to create a Git alias,
or a shell alias or function for this command right away, as you will
use it very often.
The -i
option passed to git rebase
is an alias for
--interactive
. It starts
[an 'interactive' rebase](https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt