Publicado el
Tuesday, May 12, 2020

Force push responsibly

Force pushing is not bad or wrong. It's part of the every day workflow of developers. But it must be used with caution. A force push gone wrong can overwrite existing work. Fortunately, git provides us with an option to force push responsibly.

git push --force-with-lease

Like git push --force, --force-with-lease will overwrite commits and git history. However, --force-with-lease will not overwrite any new commits a developer didn't hadn't pulled before.

Imagine you are about to use --force-with-lease to a branch. If another developer committed new work to that branch that you hadn't pulled, an attempt to git push --force-with-lease that branch will fail.

Always force push with lease

A good way of avoiding git havoc is by always force pushing with --force-with-lease. An easy and convenient way to do this is with a git alias.

My alias is git pushf. I added it to my global git config with.

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

With that alias in place, you can use git pushf to force push your work responsibly. Your coworkers and yourself will thank you for it.

Richard B. Kaufman López
Richard B. Kaufman López es un ingeniero de software. Construye aplicaciones web y lidera equipos en startups. Fuera de la oficina ayuda a miles de desarrolladores crecer de juniors a seniors.