git push force with lease
I try to avoid using git push --force
option whenever possible.
The reason is, it disables the check that verifies the remote ref is an ancestor of the local ref.
This means that if someone else meanwhile pushed his/her work upstream, it will be overwritten by the
--force
flag.
Having said that, there are some cases in which a force push is the only option, or so I thought…
Today, during a PR best practices session by the incredible Erik,
I learned about the --force-with-lease
flag.
It eliminates the risk of overwriting a team member work, by refusing to update the remote ref if its’ current value does not match our local remote-tracking branch.
The push will be rejected with "stale info"
message, and we’ll have to fetch the updates from
upstream and rebase first.
An analogy courtesy to git docs:
“It is like taking a “lease” on the ref without explicitly locking it, and the remote ref is updated only if the “lease” is still valid”.
Pro Tip
Add a git alias to easily use –force-with-lease whenever you need to push force:
$ git config --global alias.pushf "push --force-with-lease"
Source force-with-lease