Video in progress
What is the difference between `git push --force` and `git push --force-with-lease`?
--force replaces the branch on the server with your local state regardless of anything. --force-with-lease writes only if the branch on the server is in the state you last saw, and refuses if someone pushed in the meantime.
📖 Answer
The situation: you rebased a feature branch and have to force push. But in the meantime your colleague pushed a review fix to that branch.
--forcequietly deletes his commit. Nobody notices until someone asks "where is my fix?".--force-with-lease: Git compares your copy oforigin/featurewith the one on the server, and if they differ, it stops with "stale info".
git push --force-with-lease origin feature/invoice
There is one subtle point: if you ran git fetch before the push, origin/feature is already updated and the lease check passes, so your colleague's commit is deleted anyway. That is why newer Git versions have extra protection: --force-if-includes.
git config --global alias.pushf "push --force-with-lease --force-if-includes"
On protected branches like main, force push should be disabled entirely. That is a server setting.
Sign up to mark studied questions and save your progress.