Force push with care

Tute Costa

How git push force feels like

My coworker Calle (not in the picture) suggested in our guides that we prefer the flag --force-with-lease over --force to the git push command. This option allows one to force push without the risk of unintentionally overwriting someone else’s work. It will update remote references only if it has the same value as the remote-tracking branch we have locally. In other words, only if nobody has updated the branch upstream. If there are new remote commits, --force-with-lease will fail with a “stale info” message, prompting us to fetch first.

--force-with-lease fits nicely into our workflow of rebasing and squashing commits before merging into main, and we use it so often that we created an alias in our gitconfig:

[alias]
    pf = push --force-with-lease

I now always push with the pf alias, which always gets my work merged with upstream. Unless it could set the house on fire.