Safe git rebase -i for shared branches

Interactive rebasing on branches with multiple active contributors requires strict history preservation protocols. This guide enforces deterministic workflows that prevent non-fast-forward rejections and maintain CI pipeline integrity. Engineering teams must treat published history as immutable.

Diagnosing Divergence and Push Rejections

The primary symptom of unsafe history manipulation is a rejected push due to divergent commit graphs. Before initiating any mutation, verify the exact delta between local and remote refs. Fetch the latest state and inspect the topology. Identify commits that exist upstream but not locally, and confirm which local commits require linearization.

git fetch origin
git rev-list --left-right --count origin/shared-branch...HEAD
git log --oneline --graph --decorate --all

Safety Warning: Never assume local history matches the remote. Always fetch before inspecting divergence.

Pre-Rebase Isolation and Backup Protocol

Never execute history mutation on the active working branch. Create an isolated copy to preserve the original state in case of resolution failure. This isolation step is a foundational requirement within Conflict Resolution & Safe Merge Operations to guarantee zero data loss during complex graph manipulations.

git checkout -b rebase-safety-copy
git branch -M shared-branch shared-branch-pre-rebase
git checkout shared-branch

Safety Warning: The backup branch must remain untouched. Use it exclusively for forensic recovery.

Controlled Interactive Rebase Execution

Target only commits that have not yet been pulled by other contributors. Use git rebase -i origin/shared-branch to open the todo list against the verified remote tip. Apply pick, squash, fixup, or edit strictly to local-only commits. Modifying shared commits breaks downstream clones and forces manual recovery across the team. Standardize these operations using established Interactive Rebase Workflows to maintain deterministic history.

git rebase -i origin/shared-branch
EDITOR="vim" git rebase -i HEAD~5
git rebase --continue
git rebase --abort

Safety Warning: Always rebase against remote tracking refs. Never rebase against local branch tips. If conflicts arise, execute git rebase --abort immediately.

Atomic Push Enforcement with Lease Verification

After successful rebase resolution, force-push using lease semantics exclusively. --force-with-lease validates that the remote ref matches the expected SHA before overwriting. This prevents accidental destruction of concurrent teammate pushes. This atomic check is non-negotiable for shared branch operations.

git push --force-with-lease origin shared-branch
git push --force-with-lease=origin/shared-branch:refs/heads/shared-branch

Safety Warning: Prohibit git push --force in all documentation and CI configurations. Bare force overrides bypass safety checks and corrupt shared state.

Post-Push Validation and Incident Rollback

Verify CI pipeline status and run git status to confirm clean working tree alignment. If the rebase introduces merge conflicts or breaks integration tests, revert immediately using reflog navigation. Locate the pre-rebase SHA and hard-reset the branch before notifying the team of the rollback.

git reflog show shared-branch
git reset --hard <pre-rebase-sha>
git push --force-with-lease origin shared-branch

Safety Warning: Validate all SHA references via git rev-parse before executing reset operations. Incorrect SHAs permanently destroy unmerged work.

Symptom-to-Solution Reference

SymptomDiagnosisSolution
Push rejected: non-fast-forward errorLocal HEAD diverged from remote tracking branch due to concurrent commits or prior unsafe force-pushFetch remote, verify divergence, rebase against remote tip, push with lease
CI failure post-rebase due to broken commit sequenceInteractive rebase reordered dependent commits or dropped required build stepsAbort rebase, restore from safety copy, re-run with strict commit dependency mapping
Teammate reports missing commits after pullHistory rewrite altered SHAs of already-pulled commitsEnforce local-only commit rebase policy, mandate --force-with-lease, communicate SHA changes via team channel

Command Accuracy Rules

  • Always rebase against remote tracking refs (origin/branch), never against local branch tips.
  • Prohibit git push --force in all documentation and CI configurations.
  • Require git rebase --abort as the immediate fallback for unresolved conflicts.
  • Validate all SHA references via git rev-parse before executing reset operations.