Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is the golden rule of rebasing in Git?

Git
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2026
5 min read

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.

Table of Contents

  • Why rebase changes commit IDs
  • What goes wrong when you rebase shared commits
  • How to tell whether commits are shared
  • Safe and unsafe cases
  • Recovering and minimising the damage
  • Rules your team can adopt
  • Frequently Asked Questions
    • Can I rebase after pushing?
    • Does the golden rule apply to interactive rebase?
    • Is it fine to rebase onto a shared branch?
    • What if a teammate already rebased shared work?
  • Summary
  • Related Articles

Why rebase changes commit IDs

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.

bash

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.

What goes wrong when you rebase shared commits

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.

text

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.

How to tell whether commits are shared

Ask three questions before rebasing:

  1. Have I pushed these commits to a remote that others can read?
  2. Could a pull request, a CI job or a teammate be based on them?
  3. Is the branch protected, such as main, develop or a release branch?

You can also check what is local only:

bash

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.

Safe and unsafe cases

SituationRebase?
Local commits you have never pushedYes
Your own feature branch that nobody else uses, even if pushedYes, then git push --force-with-lease
A branch two people push toOnly after agreeing with them
main, master or release branchesNo
Commits already merged into a shared branchNo

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.

Recovering and minimising the damage

If you rebased something you should not have, do not panic. The old commits are still in the reflog.

bash

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.

Rules your team can adopt

A written convention prevents most accidents. Many teams agree on the following:

  • Never rebase or force push main, develop or release branches. Enable branch protection to enforce it.
  • Rebase a feature branch freely until the first review comment, then prefer new commits or merges so reviewers can see what changed.
  • Announce it in the pull request when you rebase a branch after review has started.
  • Use git push --force-with-lease instead of --force, and never force push to a branch you did not create.
  • When two people share one branch, agree who rebases and tell the other person to run 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.

Frequently Asked Questions

Can I rebase after pushing?

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.

Does the golden rule apply to interactive rebase?

Yes. git rebase -i, git commit --amend and git reset followed by new commits all rewrite history, so the same rule applies to each.

Is it fine to rebase onto a shared branch?

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.

What if a teammate already rebased shared work?

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.

Summary

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.

Related Articles

  • Git merge vs rebase: which one should you use?
  • How do you squash commits with interactive rebase?
  • Learn Git and GitHub: Complete Study Roadmap, Beginner to Pro
Share

Similar posts

  • Git merge vs rebase: which one should you use?
  • What is git rebase and how does it work?
  • How do you write a good README for a GitHub project?
  • What is a CODEOWNERS file in GitHub?
  • How do you protect a branch on GitHub?
  • What are the merge, squash and rebase options on GitHub?
  • How do you close issues automatically from a pull request?
  • How do you track work with GitHub Issues?