Git's Hidden Reflog Can Recover Commits Lost to a Force Push
A force push to a shared Git branch can silently overwrite a colleague's commits, making them unreachable from any branch on the server. The git push --force-with-lease command offers a safer alternative by rejecting the push if the remote has been updated since your last fetch, though a stale local cache can still allow destructive overwrites. What most developers do not know is that every clone retains a reflog of remote-tracking branches, recording each value a branch tip has held, including ones overwritten by force pushes. Using git reflog show origin/main, a colleague can retrieve the lost commit hash and push it back to the server with a lease to avoid further collisions. The only scenario where a commit is permanently unrecoverable is if it existed solely on the server and was never fetched by any clone, which rarely occurs in normal workflows.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in