Skip to content

Git force-with-lease: bind both commit IDs

Know exactly which old remote commit may be replaced and which reviewed candidate will replace it.

Dopstack Engineering

· 9 min read

Original conceptual editorial artwork: fixed old and new commit seals flank a replacement gate, while a separate loose HEAD tag remains outside the agreement.

Quick answer

Use an explicit force-with-lease expectation for the old remote commit and a fixed source object ID for the reviewed replacement. Background fetch can change remote-tracking refs, while local HEAD can move independently. A rejection requires inspecting the changed remote history and creating a new review decision, not refreshing the expectation and retrying automatically.

An explicit Git force-with-lease should name the remote commit you reviewed and push the candidate commit you reviewed. The lease constrains the old remote value; a fixed source object ID constrains the replacement. Capturing only the expected remote tip still leaves a later HEAD change capable of selecting a different candidate. Treat both objects as inputs to the rewrite decision. [1][2][3]

Scenario: a fictional feature/invoice-format branch needs an agreed history rewrite. Its reviewer has inspected an old remote tip, called O, and a replacement candidate, called C. A collaborator can move the remote to R. A local tool can independently move HEAD to D. These letters are explanatory labels, not usable object IDs or a report of a repository incident.

Write the expected remote object and tested candidate into the same review record. A command that consults changing branch names after review can drift away from that decision. This guide develops a narrow procedure for an owner-approved feature-branch rewrite; shared release branches and server policy remain separate constraints.

What does the lease actually compare?#

The explicit form of --force-with-lease compares the destination ref's current value with your supplied expectation. Git permits the otherwise non-fast-forward update when that condition is satisfied and rejects it when the comparison fails. It is a precondition on the remote update, not a lock held while you inspect the branch. [1]

The parameterless form derives expectations from remote-tracking information. A background fetch can update that information without integrating the new commits into your local candidate. Git's push documentation warns about exactly this interaction. The default is useful in many interactive workflows, but its comparison input is a changing local cache rather than an immutable review record. [1][2]

An explicit expectation avoids dependence on that cache after capture. It does not prove that the reviewed candidate preserves the right behavior. You can deliberately replace the expected tip with a bad commit and satisfy the lease perfectly. Remote-value equality and content review answer different questions.

Consider the fictional branch states:

Reviewed objectsState at pushExpected result or risk
Old O, candidate CRemote still O, source is fixed CLease permits the intended replacement, subject to server policy
Old O, candidate CRemote moved to RExplicit old-value lease rejects the update
Old O, candidate CRemote still O, command uses later HEAD=DLease can pass while pushing an unreviewed candidate
Old O, candidate CRemote moved away and later returned to OEquality can pass; it is not an audit of every intervening event
Decision record for the illustrative worked case.

The final row shows why equality cannot audit every intervening branch change. The explicit condition compares an object value at update time. If your workflow requires a full history of administrative changes, consult the hosting service's audit and policy facilities rather than assuming that equality encodes an event counter.

Which two objects belong in the review record?#

Record the expected remote object and the replacement candidate, along with the destination ref and the intended remote. Then perform the actual review using those fixed objects. Capturing a hash after reviewing a moving name does not establish that you reviewed that hash.

For the example feature branch, first fetch and resolve the names. rev-parse --verify and the commit peeling expression make the object being inspected explicit. The commands below are a preparation example to run only in the repository and branch covered by your rewrite agreement. They do not authorize a rewrite elsewhere. [2][3]

sh
git fetch origin
expected=$(git rev-parse --verify \
  'refs/remotes/origin/feature/invoice-format^{commit}')
candidate=$(git rev-parse --verify 'HEAD^{commit}')
printf 'Expected remote: %s\nCandidate: %s\n' "$expected" "$candidate"

Stop if either resolution fails. In a script, check those command statuses explicitly before proceeding. The values printed here should be the complete object IDs from your repository, not shortened labels copied from a diagram. Store them in the review handoff or job artifact with the remote identity and destination ref.

Now inspect the fixed values. A log of commits reachable from the remote tip but absent from the candidate helps identify work requiring an explanation. After a rebase, changed commit IDs can make a long list legitimate; it does not automatically prove that those patches disappeared. Git's log traversal and the relationship between the histories need to be interpreted in the context of the rewrite. [5]

sh
git log --oneline "$candidate..$expected"
git diff --stat "$expected" "$candidate"
git diff "$expected" "$candidate"

For a rebased patch series, compare the old and new series against their actual bases. Git's range-diff is designed to compare versions of patch series. It supplies review assistance, not proof that tests pass or every semantic change is intended. Record the bases as well if reviewers will reproduce the comparison. [4]

sh
# old_base and new_base must be resolved, reviewed commit IDs.
git range-diff "$old_base..$expected" "$new_base..$candidate"

Scenario: the invoice rewrite intentionally changes commit organization and removes an obsolete formatting patch. The reviewer records that removal rather than treating a quiet command exit as acceptance. A tree comparison can expose a missing file while a patch-series comparison explains why corresponding commits received new IDs. Both views can be useful; neither substitutes for the project's behavior checks.

Test the fixed candidate, too. If CI runs on a moving branch, retain the tested commit ID and verify that it equals the candidate in the record. Otherwise you can have a reviewed candidate, a passing run for another candidate and a push command that combines the two without noticing.

How should you prepare the explicit push?#

Use the reviewed expected value in the lease and the reviewed candidate value in the source refspec. Naming the full destination ref avoids depending on a push default or the current branch's upstream selection. Here is the narrow command for the fictional feature branch, after its rewrite review has passed. [1]

sh
git push \
  --force-with-lease="refs/heads/feature/invoice-format:$expected" \
  origin "$candidate:refs/heads/feature/invoice-format"

Compare both variables with the recorded object IDs before the push. In automation, load them from the approved job artifact rather than recomputing them immediately before pushing. If the artifact changes, the review changes. For an interactive shell, compare the printed values with the record before execution and avoid reassigning them during later exploration.

This command does not need a separate --force. Adding broad force options makes the intended protection harder to review and can affect other refs depending on the command. Keep the operation to the single destination covered by the decision. The push documentation describes both lease protection and the broader behavior of force updates. [1]

A remote named origin is also configuration, not a universal server identity. Confirm that it points to the intended repository and that its push destination matches the review agreement. In a controlled job, make that repository identity an explicit input. A correct pair of object IDs cannot stop you from targeting the wrong repository or branch if the surrounding command is misconfigured.

After the push succeeds, fetch and compare the observed destination with the candidate recorded in the handoff. Record the observation time. Someone can move the branch again afterward, so this is a readback of the completed operation rather than a promise about its permanent state. Git's fetch operation updates the information you inspect for that readback. [2]

What changes after a stale lease rejection?#

A rejection invalidates the old replacement decision until the changed remote history has been inspected. Preserve the failed expectation, candidate and destination. Fetch the remote state, resolve its new object ID and determine which commits or policy conditions caused the mismatch.

Do not automatically replace the expected value with the newest tip and retry the same candidate. That converts a failed safety condition into permission to overwrite the very work that made it fail. The newly observed remote tip is evidence for a new review, not a refreshed token that makes the previous approval reusable.

Scenario: the remote advanced from O to R because another developer fixed an invoice rounding test. The original candidate C may omit that fix. The next action is to integrate or otherwise account for it, build a replacement candidate, compare it against the new expected remote object and rerun the relevant checks. A new lease can then express the new decision.

The local drift branch is different. If the remote remains O but HEAD changes to D, the explicit command above still sends the fixed candidate C. That is its purpose. If the newer local candidate is now desired, stop and create a new candidate record rather than assuming that a later HEAD is automatically an approved improvement.

You can rehearse these cases with a bare local remote and disposable clones. Demonstrate a stale-remote rejection, a background fetch that advances tracking information, and a local branch movement after candidate capture. Then verify the actual destination object after each permitted operation. Keep such experiments outside production repositories and record the Git version and commands actually exercised.

This article's editorial checks cover command syntax and an object-transition model. It does not claim an executed remote push, a GitHub branch-protection test or a production history rewrite. That boundary matters: a model explains what to expect, while a local Git rehearsal and the real hosting service supply different evidence.

Before handing the operation to another person or job, retain the candidate's test result with its object ID. A link to the latest run on the branch is insufficient if that run can later refer to another commit. The handoff should let the recipient compare the tested candidate, the reviewed candidate and the push source without resolving a moving branch name.

If these objects disagree, stop at the disagreement. Testing the newer commit does not retroactively approve its patch changes, and reviewing the older commit does not certify the newer test result. Rebuild the evidence around one candidate. This is the local counterpart of rejecting a changed remote expectation: each input has a provenance that must survive the interval between review and action.

When should the branch stay unrevised?#

Leave it unrevised when other developers depend on its published history and no coordination agreement exists, when server policy prohibits rewriting, or when the candidate's missing work remains unexplained. A lease can make a particular update conditional; it cannot resolve ownership or reconstruct an unknown dependency on old commits.

For a shared integration branch, preserving history and adding a corrective commit is often easier to reason about. If the problem is undoing a merged change, first understand the distinction between the commit graph and the resulting tree. Our merge-revert guide explains why later reintroduction needs more than repeating the original merge.

The same discipline applies to release jobs. A queued deployment can act on an old candidate even when the newest branch looks correct. The stale deployment guide addresses that release-time check. Binding Git objects prevents one kind of drift; binding the deployed artifact to the release decision completes a different part of the workflow.

The procedure was checked against Git's current manuals as read on 7 November 2026. [1][2][3][4][5] The lease's explicit object comparison is the technical mechanism. The review tuple, stop conditions and candidate provenance record are our operating recommendation, and the project's own branch policy determines whether that operation should be allowed at all.

What else should you check?#

Does force-with-lease guarantee the candidate preserves everyone's work?
It compares a remote ref value with an expectation. That condition does not review patches, run tests or decide which work may be removed. Preserve a separate review record for the expected old object and the tested replacement candidate. [1]
Why capture the candidate instead of pushing HEAD?
HEAD can move after review because of another local action or tool. A fixed candidate object keeps the push source tied to the reviewed and tested commit. If a newer candidate is desired, create and verify a new decision record.
Should a rejected job fetch and retry with a new expectation?
Do not automatically refresh the expectation after a lease rejection. Fetch for investigation, inspect the new remote history and reconcile its work into a newly reviewed candidate. Otherwise the retry can overwrite the change that correctly caused the original rejection.
Does range-diff prove a rebase is correct?
Range-diff assists review by comparing patch series and corresponding commits. It does not establish runtime behavior or approve intentional omissions. Use the fixed bases and candidates, inspect unexplained differences and retain the application's relevant checks. [4]

Technologies

  • Git

Sources

  1. [1]
    Git - git-push Documentation, Git

    Read 2026-11-07T12:15:21.090181+00:00; publication/update date not asserted except where recorded in snapshots. Primary documentation supports the numbered claims and scoped example; not a production test.

  2. [2]
    Git - git-fetch Documentation, Git

    Read 2026-11-07T12:15:21.144531+00:00; publication/update date not asserted except where recorded in snapshots. Primary documentation supports the numbered claims and scoped example; not a production test.

  3. [3]
    Git - git-rev-parse Documentation, Git

    Read 2026-11-07T12:15:21.160200+00:00; publication/update date not asserted except where recorded in snapshots. Primary documentation supports the numbered claims and scoped example; not a production test.

  4. [4]
    Git - git-range-diff Documentation, Git

    Read 2026-11-07T12:15:21.197115+00:00; publication/update date not asserted except where recorded in snapshots. Primary documentation supports the numbered claims and scoped example; not a production test.

  5. [5]
    Git - git-log Documentation, Git

    Read 2026-11-07T12:15:20.139678+00:00; publication/update date not asserted except where recorded in snapshots. Primary documentation supports the numbered claims and scoped example; not a production test.

Written by

Dopstack Engineering

Engineering team

The engineering team at Dopstack. Illustrative scenarios are composites written to explain a pattern, not accounts of client work.

All articles Website

Get new articles by email

Engineering notes on AI, cloud and SaaS. Occasional, never sold or shared.

Send me new Dopstack Insights articles by email. I can unsubscribe at any time. We’ll email you once to confirm. Privacy

Continue reading