Learning center

Force-push a shared branch without dropping a colleague's commit

When two people push to one branch, use git push --force-with-lease --force-if-includes. The lease alone stops protecting the other person as soon as anything in your clone runs git fetch.

The lease passes after a fetch#

In the fixture, Ana pushes branch t. Ben clones it, adds a commit and pushes. Ana amends her own commit and pushes with each option in turn.

Ana's stepResult
push --force-with-lease, without fetching! [rejected] t -> t (stale info)
fetch, then push --force-with-lease --force-if-includes! [rejected] t -> t (remote ref updated since checkout)
fetch, then push --force-with-lease+ ab3009f...5fdf1cd t -> t (forced update)

After the third push, Ben's file is gone from the branch. Ana never looked at his commit. Her fetch moved origin/t to it, and the lease compares the remote with origin/t, so the two matched. Run with Git 2.43.

Editors and prompts that fetch in the background do this without a command from you. The git push manual says the option "interacts very badly with anything that implicitly runs git fetch". --force-if-includes adds a check that the remote-tracking tip was integrated into your local branch, by looking for it in the branch's reflog.

Make it the default#

git config --global push.useForceIfIncludes true

With that setting, a plain --force-with-lease includes the second check. When you know the exact commit you expect to replace, name it. The check then depends on nothing that a fetch can move:

git push --force-with-lease=t:OLD_SHA origin t

Naming the commit makes --force-if-includes a no-op, as the manual notes, so use one form or the other.

When a push was rejected#

Find out what you would have dropped before you decide:

git fetch origin
git log --oneline HEAD..origin/t

Empty output means the remote holds nothing your branch lacks by commit ID. After a rebase the list usually shows your own old commits too, so read the authors and compare patches with git range-diff. Rebase your work onto the colleague's commit, or ask them, then push.

If the commit is already gone#

Ben's clone has it: git reflog show origin/t in Ben's clone, or his local branch, names the commit. On the server, the pull request's timeline records a force push with the old and new heads. Recover by pushing the commit to a new branch, and then decide together where it belongs. Do not force the old head back, since that drops Ana's amended commit in the same way.

The handoff that avoids it#

Say who holds the branch. One sentence in the pull request is enough: "Ben pushes to this branch until Thursday; I will not rebase it." The options above catch a mistake in one clone. They do not stop two people from rewriting the same history in good faith, each having integrated the other's last push.

Worked example#

This example is fictional. Ben's test commit disappears from a pull request overnight. Ana's editor fetched every three minutes, and she pushed with --force-with-lease. The timeline shows a force push at 18:02 UTC from a head that Ben recognizes. He pushes that head to recover/tests, Ana cherry-picks the one commit, and the team sets push.useForceIfIncludes.

Return to handing over evidence.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading