TIL: Force with lease

/ Darren

Git is a useful tool for collaboration. However, we often experience conflicts when multiple people are working on the same branch.

It may happen, for example, that one developer finds that the branch should stay in sync with master and does a git rebase. Because of the rebase the developer needs to do a git push --force since her local and remote branch are based on a now-different commit history.

What if, in the meantime, the second developer pushed another commit? git push --force would overwrite that useful commit without anyone noticing.

We can restore that lost commit, but wouldn’t it be nicer if we didn’t overwrite it in the first place?

git push --force-with-lease allows just that. It is a git push --force that only pushes if the remote branch has the expected history (and not another surprise commit)

According to its documentation the behavior of --force-with-lease can be configured further. However, even with the default behavior developers should probably always git push --force-with-lease instead of just using --force.

© 2020 bitcrowd GmbH.