Sub topic 7
The rule: do not rebase commits others already have
This chapter assumes you can read git log output, know that origin/main is a remote-tracking branch, and have read the earlier chapters on how rebase replays commits with new hashes. Here you will learn why that rewriting is dangerous once other people hold the old commits.
Why the rule exists
A rebase never edits a commit. It creates new commits with the same changes but different parents, and therefore different hashes. The old commits stay in the repository until garbage collection removes them. In your own clone this is harmless, because only your branch pointer moves to the new commits.
The trouble starts when someone else already has the old commits. Git identifies commits by hash. After your rebase, your history and your teammate's history disagree about which commits exist.
What goes wrong for a teammate
Suppose you and a teammate both have feature with commits A, B, and C. You rebase it onto a newer main and force-push. Your C is now C′, a different commit with the same change. Your teammate still has C and adds commit D on top of it.
When your teammate pulls, Git sees two histories:
- The remote has A′, B′, C′.
- Their local branch has A, B, C, D.
A merge keeps both sets. The result is duplicate changes, often with conflicts between a commit and its own rewritten copy. If your teammate then pushes, the old commits you removed return to the shared branch.
Checking whether commits are shared
Before rebasing, list the commits that exist only on your machine. These are always safe to rewrite:
git log --oneline origin/feature..featureIf origin/feature does not exist, the branch was never pushed and nobody else has it. To check whether a specific commit is on any remote branch, run:
git branch -r --contains a1b2c3dOutput means the commit is published. Empty output means it exists only locally. Run git fetch first, or the remote-tracking branches may be out of date.
When rebasing published commits is acceptable
- A personal branch. You are the only person who pushes to it, and nobody builds on it. You will need to force-push, which the next chapter covers.
- A team agreement. Some teams rebase feature branches by convention and tell everyone to expect it.
- Review branches. Many workflows expect you to tidy a pull request branch before merge.
Never rewrite shared long-lived branches such as main or release/*. Everyone's work is based on them.
If you already rebased shared commits
Tell the people affected before they pull. Recovery is easier before they have committed on top of the old history. If you can restore the original history, run git reflog and reset to the old tip, as shown in the recovery chapter. That avoids asking everyone else to repair their branches.
Exercise
You and a colleague both have commits A, B, C on feature. You rebase onto main and force-push. Your colleague commits D on top of their old C, then runs git pull. Why do they see duplicate changes?
Show answer
Your rebase replaced A, B, C with new commits A′, B′, C′ that have different hashes. Your colleague's branch still contains the old commits, so Git treats the remote's commits as new work. The merge combines both sets, and the same changes appear twice, often with conflicts.