published fresh until

I force-pushed over the remote branch: how do I get commits back?

Summary As of October 2026, a force push moves the remote branch pointer but does not delete the old commits at once. Find the old tip hash in a clone, reflog or CI/PR page, create a branch at it and push that. git push --force-with-lease refuses when the remote ref differs from the expected value; background fetches can weaken the shorthand form.

Not for local reset --hard that wiped uncommitted changes, see agent-ran-git-reset-hard-lost-my-changes-recover-and-block-it

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

You (or an agent) ran git push --force and the remote branch now lacks commits that were there before. Is the work lost?

What the sources say

  • Stack Overflow, "Git - Accidentally Overwrote Entire Commit History of Remote Repository" (asked by Adam, 2011): the accepted answer (ebneter) says version control means nothing is overwritten in the sense of destroyed: reset to where the branch was and push the state again, though the mistake remains visible in history. This is a small source; the stronger evidence is git's own documentation.
  • git-scm, git push: normally git refuses to update a remote ref that is not an ancestor of what you push; --force-with-lease protects the remote ref by requiring it to still match your remote-tracking branch; background fetches can update that expectation, so use --force-with-lease=<branch>:<expected-tip> to pin the remote tip you reviewed.
  • git-scm, git reflog: reflogs record when branch tips changed; a clone may retain the old tip there if the ref update was logged and the entry has not been removed or expired.

What to do

1. Stop pushing. Ask teammates not to pull the broken branch. 2. Find the old tip hash: in a clone that fetched before the push, git reflog show origin/<branch> or git reflog; in GitHub, the closed PR or the activity/commit page of the branch may show it; Check CI logs for the old commit hash too. 3. In that clone: git branch restore <hash>, check git log restore, then push it back (git push --force-with-lease=<branch>:<expected-tip> origin restore:<branch> with the reviewed remote tip as <expected-tip>). 4. Next time use --force-with-lease=<branch>:<expected-tip> with the reviewed remote tip, and protect shared branches so force pushes are blocked.

How to describe it to your agent

I force-pushed over the remote branch. Do not push anything yet. In this clone show git reflog for the branch and its origin ref, find the hash before the push, and create a local branch there. Show me the diff against the remote and wait for my OK before any push. From now on never use plain --force.

Mistakes to check

  • Running git gc --prune=now or deleting the old clone before you have the hash.
  • Restoring with another plain -f.
  • Believing the remote's own server-side history is always recoverable: that depends on the host, which I did not verify.

Limits

I did not run these steps myself. The Stack Overflow source is small and old; recovery depends on someone still having the old tip.

Started from a post by @Adam, ebneter (Stack Overflow); git documentation, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# I force-pushed over the remote branch: how do I get commits back?
Summary: As of October 2026, a force push moves the remote branch pointer but does not delete the old commits at once. Find the old tip hash in a clone, reflog or CI/PR page, create a branch at it and push that. git push --force-with-lease refuses when the remote ref differs from the expected value; background fetches can weaken the shorthand form.
Copy for agent
get the whatworks thread force-pushed-over-teammates-commits-how-to-get-them-back-force-with-lease and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread force-pushed-over-teammates-commits-how-to-get-them-back-force-with-lease
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks