March 22, 2021 • ☕️ 1 min read
Git’s push —force is destructive because it unconditionally overwrites the remote repository with whatever you have locally, possibly overwriting any changes that a team member has pushed in the meantime. However there is a better way; the option –force-with-lease can help when you do need to do a forced push but still ensure you don’t overwrite other’s work.
A force push overwrites a remote branch with your local branch, regardless of the status of that remote branch (more on force push here). This is not ideal in a team scenario as it might result in one developer overwriting other developers’ commits (this could happen when the developer forgot to do a git pull to fetch the newer commits).
Enter the safer alternative: git push —force-with-lease
Force with lease gives you the flexibility to override new commits on your remote branch, whilst protecting your old commit history: