Hone

Lessons · Git · what force does to everybody else

What force does to everybody else

A normal push only fast-forwards. --force says the branch is whatever you have, and anything on the remote that is not in your history stops being on the branch.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

--force-with-lease refuses when the remote has moved since your last fetch, which is exactly the case where forcing would drop somebody's work.

How to think about it

Fetch, look at what each side has, then decide. If you must rewrite a shared branch, --force-with-lease is the version that can tell you no.

Worked example

git log --oneline
One commit here, and it is not the one the remote has.
git push --force-with-lease origin main
Refused: stale info. The remote moved since you last looked.
git fetch -q origin
So look.
git log --oneline origin/main
There is their commit. Forcing would have removed it.

Your turn

Find out what the remote has, before deciding anything.

git  -q origin

The trap

Reaching for plain --force when the lease refuses. The refusal is the tool telling you there is work you have not seen.

Practise what force does to everybody else on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.