Force-push a branch safely

Destructive
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Overwrites a remote branch with rewritten local history using --force-with-lease, which refuses to clobber commits you have not seen.

gitpushforcehistory

Confirmation required

This recipe is destructive and requires confirmation — confirm you understand the impact before running any command below.

Parameters

Branch to force-push.

Commands

Review the local commits that will replace the remote history

git log --oneline origin/<branch_name>..<branch_name>

Overwrite the remote branch, aborting if it moved since the last fetch

git push --force-with-lease origin <branch_name>

Verification

git ls-remote origin <branch_name>

The remote branch hash matches the local branch tip.

Undo

Not undoable

The remote's previous commits are no longer referenced there. They can sometimes be restored from another clone or the local reflog by pushing the old tip again, but there is no guaranteed recovery.

Pitfalls

  • Never force-push a shared branch; collaborators' work based on the old history is orphaned.
  • --force-with-lease is only safe if you have not run git fetch since reviewing; fetching updates the lease and disarms the check.
  • Prefer this over plain --force in every case.

Related