Git & GitHub Interview Question #141
What is the difference between --force and --force-with-lease?
Security, Team Workflows, Troubleshooting & Senior Git/GitHub Scenarios Senior Advanced
Quick Interview Answer
--force overwrites the remote branch reference without checking whether it changed since your last view. --force-with-lease adds a safety check and normally refuses the update if the remote reference no longer matches the value you expected.
Detailed Explanation
--force overwrites the remote branch reference without checking whether it changed since your last view. --force-with-lease adds a safety check and normally refuses the update if the remote reference no longer matches the value you expected.
When a rewritten feature branch genuinely must be pushed, --force-with-lease is generally safer because it helps avoid overwriting another developer's newly published work. It is still a history rewrite and should be used deliberately.
Code Example
git fetch origin
# Safer than plain --force for an intentional
# rewritten feature branch:
git push --force-with-lease origin feature/search