Recover lost commits from git push –force

white jigsaw puzzle illustration

Today I learned how to retrieve accidentally lost commits pushed to remote (via –force). I accidentally overwrote a co-worker’s committed changes and my own revisions on his branch using git push --force origin/branch!

I freaked out at first realizing how grave the mistake I made. I panicked at first, like any child would panic after breaking something. Then, I paused and reflected on the problem and just figured out how to get the code back.

I followed this Stack Overflow discussion; and the summary of the commands are as follows:


# retrieves dangling SHAs, commits on your local machine that may have already been removed on remote
git reflog show remote/origin/<your-branch-name> 

# see if you can still retrieve your lost commits available on your local machine that are no longer available on remote
git checkout <some commit SHA> 

# if you found your commits and were able to check it out, great! You've recovered your changes
git checkout -b feature/<your feature name> 

Word cloud

Accidentally deleting commits, remote repository commit changes. Local copy of git branches, local copy of git commits. Data recovery. Source code recovery.


How can I recover from an erronous git push -f origin master?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.