Don’t Git Push --force!

Don’t Git Push --force!

Nicholas Lewanowicz's photo
Nicholas Lewanowicz

Published on Jan 8, 2021

2 min read

Subscribe to my newsletter and never miss my upcoming articles

At some point every developer has used --force and overwritten somebody else’s work unnecessarily. It’s usually not the end of the world but its easily avoidable.

IMO you should never really use --force, in virtually 99.9% of cases where you need to force push you should be using --force-with-lease.

force vs with-lease

--force: Overwrites changes to your remote with the changes from your local irrespective of what changes exist on the remote branch.

--force-with-lease: Overwrites changes to your remote with changes from your local only if there have been no new commits to the remote branch (for example from a coworker or CI/CD bot).

ALIAS’!

If you haven’t been introduced to git alias’ then its time.

git config --global alias.pushf "push --force-with-lease"

source

The above alias will not only make your force pushes safer but also save you keystrokes.

Conclusion

It’s clear that --force-with lease is the better option when your working on a branch with other people but even if your the only person pushing changes to a branch you may still want to use --force-with lease. I have worked at multiple companies where there would be an automated process for generating a change log or generating translations and they would be committed against the PRs before getting merged, on multiple occasions folks would unintentionally force push and erase those commits.

Hope you enjoyed this article feel free to check out my other tips, follow me, or subscribe to my newsletter.

👋 Have a great day!

 
Share this