Git Interview Question 7: Force with Lease Background Fetch
Git Interview Questions · Test 1 · question 7 of 10
You and a colleague both push to feature/api. You rebase your local feature/api onto main. While you work, your colleague pushes a new commit X to origin/feature/api. Your IDE runs git fetch origin in the background every few minutes, and it fetches X without you noticing. You never look at X or integrate it.
You then run git push --force-with-lease with no other arguments. What happens?
Show answer and explanation
Answer:
Run without an expected value, --force-with-lease allows the force push only if the remote ref still matches your local remote-tracking branch (origin/feature/api). The background fetch already moved origin/feature/api to X, so the remote matches it, the lease check passes, and the push overwrites X. The git-push documentation specifically warns that this form "interacts very badly" with anything that runs git fetch in the background. In that case the protection is "trivially defeated." To prevent this, pass an explicit expected value (--force-with-lease=feature/api:<sha>) or add --force-if-includes. That option only allows the force update if the remote-tracking tip has been integrated into your local branch.
Option A describes what the lease would do if the remote had moved and you hadn't fetched. The lease compares against the remote-tracking ref. It doesn't check whether your branch contains the remote's commits.
Option B is wrong. git push never merges or rewrites your local branch. It only updates refs on the remote.
Option C refers to the reflog check that --force-if-includes adds. Plain --force-with-lease doesn't look at your local reflog.
Reference: git-push