Lesson Completion
Back to course

Safe History Rules

Beginner
8 minutes4.7Git

The Hook (The "Byte-Sized" Intro)

There's one rule that separates competent Git users from dangerous ones: never rewrite history that others depend on. Every team horror story — force-pushed main, lost sprint work, broken CI — traces back to violating this rule. These 5 rules take 2 minutes to memorize and save years of pain.

📖 What are Safe History Rules?

These are guidelines that determine when it's safe to rewrite Git history and when it's not. Following them prevents data loss and team disruption.

Conceptual Clarity

The 5 golden rules:

#RuleWhy
1Only rewrite unpushed commitsPushed commits are shared property
2Never force-push to main/developBreaks everyone's clone
3Use --force-with-lease instead of --forceChecks for others' work before overwriting
4Create a backup branch before risky operationsCheap insurance
5Use revert on shared branches, reset on localAdditive changes are always safe

The safety spectrum:

OperationRisk LevelSafe On Shared?
git revert🟢 None✅ Always
git cherry-pick🟢 None✅ Always
git commit --amend🟡 Low❌ Only before push
git rebase🟠 Medium❌ Only personal branches
git reset --soft/mixed🟠 Medium❌ Only before push
git reset --hard🔴 High❌ Only before push
git push --force🔴 Critical❌ Never on shared

Real-Life Analogy

Safe history rules are like firearm safety rules: always treat history as shared (loaded), never point --force at a shared branch (someone), and always create a backup (safety) before operating.

Visual Architecture

flowchart TD ACTION["Want to change history?"] --> LOCAL{"Local only?"} LOCAL -->|"Yes"| FREE["✅ Rewrite freely<br/>reset, rebase, amend"] LOCAL -->|"No"| SHARED{"Shared branch?"} SHARED -->|"Personal branch"| LEASE["⚠️ Use --force-with-lease"] SHARED -->|"main/develop"| REVERT["🛑 Use revert only"] style FREE fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb style LEASE fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style REVERT fill:#2d1b1b,stroke:#e94560,color:#e94560

Why It Matters

  • Team trust: Following these rules means no one ever loses work because of you.
  • Branch protection: Many companies enforce these rules via branch protection settings.
  • Career: Force-pushing to main is a widely-known anti-pattern, tested in interviews.
  • Recovery cost: Prevention takes seconds; recovery takes hours.

Code

bash
# ─── Rule 1: Only rewrite unpushed commits ─── git log --oneline origin/main..main # Shows unpushed commits # If empty → nothing is unpushed → safe to rewrite # ─── Rule 3: Use --force-with-lease ─── git push --force-with-lease origin feature/login # Checks if remote has new commits you don't have # Refuses to push if someone else pushed in the meantime # ─── Rule 4: Backup branch before risky ops ─── git branch backup-before-rebase git rebase -i HEAD~5 # If something goes wrong: git reset --hard backup-before-rebase # ─── Rule 5: Revert on shared, reset on local ─── # Shared branch (main): git revert abc1234 # Local branch (not pushed): git reset --soft HEAD~1

Key Takeaways

  • Never rewrite pushed commits on shared branches — use revert instead.
  • Always use --force-with-lease instead of bare --force.
  • Create a backup branch before rebasing or resetting.
  • Learn the safety spectrum — know which operations are safe where.

Interview Prep

  • Q: What is --force-with-lease and why is it safer than --force? A: --force-with-lease overwrites the remote branch only if it matches your expected state. If someone else pushed commits you don't have, it refuses to push — preventing you from accidentally deleting their work. --force overwrites unconditionally.

  • Q: How would you undo a commit on a shared branch? A: Use git revert <sha>. This creates a new commit that undoes the changes without rewriting history. Never use git reset on shared branches because it would require force-pushing, which breaks other developers' clones.

  • Q: What precaution should you take before an interactive rebase? A: Create a backup branch first: git branch backup-before-rebase. If the rebase goes wrong, you can restore your branch with git reset --hard backup-before-rebase. This is cheap insurance against mistakes.

Topics Covered

Git HistoryGit Best Practices

Tags

#git#history#safety#best-practices

Last Updated

2026-02-13