Sub topic 10
Closing the gap with --force-if-includes
This chapter assumes you know what a remote-tracking branch such as origin/feature/login is, how git rebase rewrites history, and how --force-with-lease compares the remote branch with your remote-tracking branch. Earlier chapters covered those. Here we look at a gap in that check and at the option that closes it.
The gap in the lease check
--force-with-lease asks one question: does the remote branch still match my remote-tracking branch? If it does, the push goes ahead.
That check says nothing about whether you have looked at the commits behind that remote-tracking branch. A git fetch moves origin/feature/login to match the remote. An IDE's auto-fetch does the same, silently. After either one, the remote and your remote-tracking branch agree, so the lease passes. Your local branch may still lack the teammate's new commits, and the force push then deletes them.
What --force-if-includes checks
--force-if-includes adds a second condition. Before the push, Git looks at the reflog of your remote-tracking branch. It confirms that every commit the remote-tracking branch has pointed to is reachable from your local branch tip. In plain terms: you must have integrated what you fetched.
It only works together with --force-with-lease. Git ignores it when the lease is absent or when you pin an explicit expected value, so use it in the plain form shown here:
git push --force-with-lease --force-if-includes origin feature/loginNow the same scenario plays out differently. The fetch moved origin/feature/login to commit C. Your local branch does not contain C. The lease passes, but the includes check fails and Git refuses the push.
Recovering from a refusal
When the push is refused, treat it as you would a stale info rejection. Fetch, look at what you were missing, integrate it, and retry.
git fetch origin
git log --oneline HEAD..origin/feature/login
git rebase origin/feature/login
git push --force-with-lease --force-if-includes origin feature/loginThe git log line lists the commits on the remote branch that your local branch does not have. Read them before you rebase. If they are unexpected, ask the author before you continue.
After the rebase, your local branch contains the teammate's commit, so the includes check passes and the push succeeds.
Making it the habit
Typing both flags each time is tedious, so people skip them. An alias makes the safe form the short one:
git config --global alias.pushf "push --force-with-lease --force-if-includes"
git pushf origin feature/loginYou can also set push.useForceIfIncludes to true in your Git config. Git then adds the includes check to every --force-with-lease push without a flag.
git config --global push.useForceIfIncludes trueLimits to remember
--force-if-includesneeds a reasonably recent Git (2.30 or later).- It relies on the reflog of the remote-tracking branch. If the reflog is missing or has expired, the check cannot do its job as designed.
- It checks that you integrated fetched commits. It does not check commits that were never fetched. The lease still covers that case, because an unfetched remote change makes the remote differ from
origin/feature/login. - Neither option replaces communication. On a branch several people push to, tell them before you rewrite it.
| Situation | Lease alone | Lease with includes |
|---|---|---|
| Teammate pushed, you have not fetched | Rejected (stale info) | Rejected (stale info) |
| Teammate pushed, background fetch ran, you did not integrate | Passes, commits lost | Rejected |
| Teammate pushed, you fetched and rebased onto it | Passes | Passes |
Exercise
You run git push --force-with-lease origin feature/login and it succeeds, but a teammate later says their commit vanished from the branch. Your IDE auto-fetches every few minutes. What probably happened, and which option would have prevented it?
Show answer
The teammate pushed, then the IDE's auto-fetch updated origin/feature/login to include their commit. The remote then matched your remote-tracking branch, so the lease passed, even though you never integrated the commit into your local branch. Adding --force-if-includes (or setting push.useForceIfIncludes to true) would have refused the push, because the fetched commit was not reachable from your local branch.
Exercise
Your push with --force-with-lease --force-if-includes is refused. What commands do you run to find out what you were missing and to recover?
Show answer
Run git fetch origin, then git log --oneline HEAD..origin/feature/login to list the missing commits. Review them, run git rebase origin/feature/login to integrate them, and push again with git push --force-with-lease --force-if-includes origin feature/login.