Git rebase is a surprisingly large topic. And just the command itself
can take a while to learn to become comfortable with. And then after
that you have to consider the social aspect of it (like you are
doing). So let’s simplify things so that we can discuss a strategy for
rebasing which is, I would claim, very safe.
Prerequisite: do you need it?
Git rebase is an elegant tool, but it is also optional. You don’t
strictly need it.
Rebasing as an individual contributor on your feature branches
You can use rebase to rewrite your own branches during development and
before merging into mainline.
In the first phase of development your branch might be private. Then
rebasing is not a problem since the history has not been shared. So
rebase as much you need.
This doesn’t just mean to tidy up your history but also to rebase on top
of updates to the main branch (the merge alternative is to merge
mainline into your own branch periodically).
The next phase might be the pull request phase. Here some care and
consideration is warranted. As you gain experience you can be more
flexible at this point. But as a relative beginner you can follow this
rule:
- Don’t rebase after you have published the PR
Note that this is only a temporary disabling of this feature (see next
point). So you can still make (what are called) “fixup” and “squash”
commits (read man git rebase). But you don’t use rebase yet, just in
case reviewers are checking out your branch.
Like already mentioned, the merge-variant of updating your branch
according to the main branch is to merge main into your own branch
periodically (a “back merge”). This might be necessary if your pull
request ends up living for more than a week.
The final phase is the pull request approved phase. Now you can rebase
your branch again. Why? To clean up your history. And why is it okay to
do so? Because your team has decided that your changes can be merged
into mainline; it’s about to be merged in (by you) and so it will be
deleted soon and will not matter to anyone.
One precaution to this, though: some people might have based their work
on top of yours (they should have told you about this beforehand, by the
way, so you should be in the know (or else it’s on them)). It might be
easier for them to continue their work if you merge into the main branch
directly instead of rebase + merge, since they will need to update their
branch to main (their new base). I have no experience with this
particular scenario so I can’t say for sure.
So rewriting history before merging into the main branch is generally
fine.
Postscript: on rewriting published-but-solo branches
The aforementioned strategy is meant to be simple and straightforward
and to avoid arguments and headaches caused by “you changed the branch
out from under me”.
Again, as you gain more experience you can be more flexible. Is “public
history” really sacred? No, not at all; you might have a tacit
understanding that, say, branches in pull requests are owned by the
author and not something that other people are supposed to base their
own work on willy-nilly. And in that case the contributor has the
freedom to rewrite their pull request branches at will.
Postscript 2: People who cannot rebase
This has covered a possible workflow for an individual
contributor. Individual contributors have the most freedom when it comes
rewriting history since they are often leafs in the development
graph. People who practically cannot use rebase are the “integrators”
(in Git SCM-speak), or perhaps more commonly known as “maintainers”,
since there might be hundreds or thousands of people who are doing
development based on their trees (their histories).
is to simply avoid using rebase altogether in the team, with us all being Git novicesUnless you are all experts or well-versed in git, I would still advise against rebasing branches no matter how expert you or half of the team is. It only takes one of you to cause the chaos.