Full Byte Developer.

git push --force can delete your teammate's commits. Use this instead.

One flag and one setting stop a plain force push from wiping commits you never saw.

Published here · 2 sources

A plain force push wiped a teammate's fix off the shared branch in a test repo. One flag and one setting stop it.

The fix

bash
git push --force-with-lease
git config --global push.useForceIfIncludes true

How it works

Step 1: Swap --force for --force-with-lease. It refuses if the remote moved since your last fetch [1].

Step 2: Set push.useForceIfIncludes once. If your editor fetched in the background, the lease alone passes because Git thinks you have seen the new commit. This setting also checks that you actually integrated the remote commit [2].

Step 3: Read the rejection. Pull or rebase onto the new commit before you push again.

What we ran

Git 2.54.0, bare repo plus two clones, no network:

  • --force: + 4d85209...de09ed6 main -> main (forced update). The fix was gone from origin.
  • --force-with-lease: ! [rejected] main -> main (stale info). Nothing lost.
  • After a background fetch, --force-with-lease passed.
  • With push.useForceIfIncludes true: ! [rejected] main -> main (remote ref updated since checkout). The gap closed.

The catch

The setting only adds protection to --force-with-lease. It does nothing for plain --force. The teammate's commit was still in their own clone, so it is recoverable, but nobody else on the branch sees it until they push again.

Optional alias

bash
git config --global alias.pushf "push --force-with-lease"

Send this to the teammate who force pushes to the shared branch without a second thought.

Keep this guide

By Full Byte Developer

Filed under Dev tools

Questions about this guide

Does the setting work with plain --force?

No. It only adds protection to --force-with-lease. Plain --force ignores it.

What if my editor fetches in the background?

The lease alone passes. The setting closes that gap by checking that you actually integrated the remote commit.

Sources

Next in this stack