How to Recover an Overwritten Commit After a --force-push

Mae Capozzi · November 5, 2021Have you ever accidentally overwritten a colleague's work with a force push? Here's an easy way to recover without anyone ever having to know.

The other day I was working on a git branch, and I wanted some feedback from a colleague. We work in different time zones, so after I logged off for the night, he pushed a commit to my branch to suggest an alternative approach.

Over the course of the next 13 hours I forgot that he had added a commit to my branch. I only realized that I had overwritten his work (and modified the git history in the processs) after the fact.

Normally, I would've just sent him a message asking him to push up his local copy of the branch again, but he happened to be at an appointment. In the course of trying to restore the lost changes, I learned that Github's tree URLs support reflog syntax.

I was able to find my colleague's changes at https://github.com<org-name>/<repo-name>/tree/<branch-name>@{2}. This allowed me to look up the hash of his commit so that I could reset it.

It's good to know how to solve this problem if you run into it, but there's an even better way to avoid the problem in the first place. Git offers a --force-with-lease flag that checks to see if anyone else has pushed to your branch before overwriting the branch with your changes.

To make this easier so that I don't have to remember to type --force-with-lease every time, I usually set up an alias like this one:



Thanks to Tim Brown @brimtown for originally encouraging me to set up an alias after I overwrote his work in 2019 or something.

Continue Reading

aitypescriptdeveloper-tools

Tooling migrations don't have to take weeks anymore

I used to block out weeks for tooling migrations. Now I let Claude Code run in the background, check in when it's done, and pair with it to understand what changed.

Read Post
frontend platformsystems thinkingobservability

Frontend Technical Debt Is a Business Problem

Frontend technical debt doesn't just slow down developers. It undermines your entire business through user attrition, legal risks, and revenue loss. Here's how to spot and communicate these hidden costs.

Read Post
aideveloper-tools

AI-Assisted Dependency Review

How I used Conductor and Claude to streamline my team's Dependabot review workflow — and how a colleague turned it into a GitHub Action

Read Post