How to push force with security

Git push force is needed in many situations where you re-write the git history. However, it can be dangerous if you didn’t check if any of your colleagues pushed something on your current branch in the meantime. The result with a git push –force is that your changes on your branch will be pushed and will erase any current version that is different from yours.

Hopefully, there is an option to use to avoid these kinds of problems. I wish I knew that a few years ago when a senior dev colleague yelled at me because I pushed forced on top of his latest commit.

The command to use is the following :

git push --force-with-lease

This command will push force if no one pushed something in between. But if it sees any new commits, it will display a warning message and abort the push. Be sure to use this if you are working on the same branch with other colleagues to avoid any unnecessary accidents.

Hope it was useful.

TheTrendyBrand