AI Hints
Git & GitHub · Lesson 88 of 144

The Golden Rule of Rebase

Never rebase shared commits.

⏱ 2 min read▶ Video at 1:36:00🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Rebase creates new commits, and throws away the old ones. That's fine for commits only you have. But if teammates already have the old commits, rebasing causes chaos.

The Golden Rule of Rebase
From the video: The Golden Rule of Rebase
  • Rebase replaces commits
  • Fine if only you have them
  • Chaos if others do

The golden rule

So here's the golden rule. Never rebase commits that other people have already pulled. Rebase your own local work as much as you like. But once commits are on a shared branch like main, leave them alone, and use merge or revert instead.

Your local workrebase freely
Shared commitsnever rebase
Shared branchmerge or revert
If in doubtmerge

When to use which

In practice, many people rebase their own feature branch onto main, to keep it fresh, before opening a pull request. Then the pull request is merged normally. That way, you get a clean history, and nobody's work is ever rewritten.

Rebase

  • Your own feature branch
  • Before opening a PR
  • To catch up with main

Merge

  • Shared branches
  • Merging pull requests
  • When unsure

Summary

Never rebase commits others already have. Rebase your own work, merge shared work. Next, rewriting your own recent commits, before you share them.

Key takeaways
  • Never rebase shared commits
  • Rebase your own branch
  • Merge shared work
Next up: Cleaning Up Commits — interactive rebase