git push --force and how to deal with it
![Cover for git push --force and how to deal with it](/static/16814876d8b89e024ffd058adad6636f/21b98/cover.jpg)
Topics
Translations
Ever been in a situation where the wrong Git command has had a chaotic impact on your project’s repo? People make mistakes! …but sometimes, these mistakes can cost hours of your team’s time. So, in this post, we’ll show you how to quickly recover from an unfortunate git push --force
.
Editor’s note: This post was originally published in 2017, but it’s been overhauled for 2024!
Now, if you’re thinking, “this doesn’t sound like something I’d ever do”, well, let’s not get overly confident. Sooner or later, this is going to happen. When working with several remotes in the same Git repository, eventually, you will git push --force
into main
(or another important branch that should’ve never been messed with).
This might happen, for instance, when deploying with Heroku, which uses separate Git remotes to build and deploy an application. Following a long day of work, it could be incredibly easy to execute git push --force
instead of the usual git push --force heroku main
.