The golden rule of rebasing in Git is: never rebase commits that exist outside your own repository, meaning commits other people may have based work on. Rebasing rewrites commits into new ones with new IDs, so doing it to shared history forces everyone else to untangle the difference. Rebase only local, private commits.
This rule is the reason people say rebase is dangerous. The command itself is safe; the risk is applying it to the wrong commits.
A commit ID is a hash of its content, its author data, its message and, crucially, the ID of its parent. When you rebase, Git changes the parent of each commit, so every rebased commit gets a different hash even if the file changes are identical.
To Git, a1b2c3d and 4e5f6a7 are two unrelated commits that happen to have the same change and message. The old ones are not edited. They are abandoned and the branch points to the copies.
Suppose you push feature to the remote and a teammate pulls it and adds a commit on top. You then rebase feature and force push.
When the teammate runs git pull, Git sees two histories that both contain a version of the same work. A merge brings the old commits B and C back next to B' and C', so the history contains duplicates, conflicts appear for changes that were already resolved, and the teammate's commit D can be lost or applied twice. Fixing it takes manual work for everyone involved.
Ask three questions before rebasing:
main, develop or a release branch?You can also check what is local only:
This lists commits that are on your feature branch but not on the remote copy. Those are safe to rewrite. Commits that already appear on origin/feature are shared, even if you are the only person who has pulled them so far.
| Situation | Rebase? |
|---|---|
| Local commits you have never pushed | Yes |
| Your own feature branch that nobody else uses, even if pushed | Yes, then git push --force-with-lease |
| A branch two people push to | Only after agreeing with them |
main, master or release branches | No |
| Commits already merged into a shared branch | No |
The second row is a common exception. The rule is really about other people, not about whether a push happened. A pull request branch that only you use can be rebased, as long as you tell reviewers when you do.
If you rebased something you should not have, do not panic. The old commits are still in the reflog.
Pick the reflog entry from before the rebase. Resetting is fine here because the damage is in your local branch; if you already force pushed, coordinate with your team before pushing again.
Two habits reduce the risk. Use git push --force-with-lease rather than --force, because it refuses to overwrite commits you have not seen. Many hosting platforms also let you protect branches so force pushes to them are rejected outright.
A written convention prevents most accidents. Many teams agree on the following:
main, develop or release branches. Enable branch protection to enforce it.git push --force-with-lease instead of --force, and never force push to a branch you did not create.git fetch and git reset --hard origin/branch afterwards, after saving any local work.These habits cost little and remove the situations in which the golden rule is broken by accident.
Yes, if you are certain nobody else has based work on the pushed commits. You will need to force push, preferably with --force-with-lease.
Yes. git rebase -i, git commit --amend and git reset followed by new commits all rewrite history, so the same rule applies to each.
Yes. The rule concerns the commits being moved, not the branch they land on. Rebasing your private branch onto origin/main does not rewrite main.
Do not merge their rewritten branch blindly. Save your own commits, then use git rebase --onto to move them onto the new history, or ask the teammate to coordinate.
Rebase creates new commits with new IDs, so using it on commits that others already have splits history and causes duplicates and conflicts. Keep it for local or private work, check what is unpushed with git log origin/feature..feature, force push only with --force-with-lease, and use the reflog to recover from mistakes.