Force pushing safely

Intermediate · 9 min

Why a rewritten branch needs a force push, why --force-with-lease beats --force, and the one case where even the lease lets you overwrite a teammate.

Before thisUpstream tracking branchesAmending the last commitRebasing a branch

Dave Liddament · 9 min

Force pushing safely

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Dave LiddamentReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Rewriting commits that were already pushed (amend, rebase, reset) gives them new hashes, so a normal push is rejected and only a force push can replace the remote branch.
  • `--force-with-lease` refuses to overwrite the remote branch if it moved since your last fetch, which `--force` never checks.
  • The lease compares the remote against your local `origin/<branch>`, so a fetch that you didn't read through renews the lease and lets you overwrite a teammate's commit.
  • `--force-if-includes` adds a check that your branch actually contains the remote-tracking commit, which closes that gap.
  • Force push only to your own feature branches, never to shared branches such as `main`, and tell people who share the branch before you do.

Why history rewrites need force

Every commit’s hash covers its content and its parent. When you amend a commit, or rebase a branch onto new work, Git builds new commits and abandons the old ones. The abandoned commits still exist on the remote, and the remote’s branch points at them.

Now a normal push fails. Pushing is allowed only when the remote’s branch tip is an ancestor of what you send, a fast-forward. After a rewrite, the old tip is not an ancestor. In the jot repository I pushed a branch with two commits, then amended the second:

$ git push -u origin rename-note
$ git commit --amend -m "Add note ids and tests"
$ git push
To ../hub.git
 ! [rejected]        rename-note -> rename-note (non-fast-forward)
error: failed to push some refs to '../hub.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. ...

The hint says “use git pull”, which is the wrong advice here, because pulling would merge the old commit you just replaced back in. What you actually want is to replace the remote branch with your rewritten one. That is a force push.

--force versus --force-with-lease

git push --force overwrites the remote branch with whatever you have, with no questions. If a teammate pushed a commit to that branch an hour ago and you never fetched it, it is gone from the remote.

--force-with-lease adds one condition: “overwrite only if the remote branch is still where I last saw it”. Git compares the remote’s actual tip with your local origin/rename-note.

$ git push --force-with-lease
To ../hub.git
 + 2df0b33...4d4a559 rename-note -> rename-note (forced update)

The + and ... mark a forced update. Now the case it exists for. A teammate pushes a commit to the same branch while I amend again:

$ git commit --amend -m "Add note ids, tests and polish"
$ git push --force-with-lease
To ../hub.git
 ! [rejected]        rename-note -> rename-note (stale info)
error: failed to push some refs to '../hub.git'

stale info means my picture of the remote was out of date, so Git refused. A plain --force would have silently deleted my teammate’s commit. (Hashes will vary on your machine.)

The gap: fetching renews the lease

Here is the part many tutorials skip. I took the next obvious step and ran git fetch. Then I pushed again:

$ git fetch
$ git push --force-with-lease
To ../hub.git
 + 6e0cd96...739443b rename-note -> rename-note (forced update)

It succeeded, and my teammate’s “docs for ids” commit was dropped from the remote branch. The fetch moved my origin/rename-note to their commit, so the lease saw “the remote is exactly where I last saw it” and allowed the overwrite. The lease protects against surprises you haven’t seen. It can’t protect against ones you fetched and ignored. Some editors and tools run git fetch in the background, which makes this easier to hit than it sounds.

Two defenses:

$ git fetch
$ git push --force-with-lease --force-if-includes
To ../hub.git
 ! [rejected]        rename-note -> rename-note (remote ref updated since checkout)

--force-if-includes requires that the remote-tracking commit is actually part of your branch’s history. A fetched-but-never-integrated commit isn’t, so the push is refused. The other defense is explicit: --force-with-lease=rename-note:<sha> states the exact commit you believe is on the remote. It is rejected, with stale info, once anyone has moved it. Both are worth a shell alias.

Rules that keep force pushes boring

  • Force push your own feature branches. A branch only you use has no one to surprise.
  • Tell collaborators before rewriting a shared branch, and expect them to reset or rebase onto the new history.
  • Never force push main, release branches, or anything others build on. Undo a published mistake with reverting a commit.
  • Lost something? The overwritten commit is still in your teammate’s clone, and often in your own reflog.

Hosting platforms can block force pushes to important branches with protection rules, which is a later lesson in this roadmap.

What the video covers

Dave Liddament’s “Git tips and tricks. Day 24: git push –force-with-lease” is about nine minutes, from early 2026, part of a daily tips series. Going by its title, it covers the safer force-push flag. I judge English from the title, description and channel only, and I could not read a transcript. I don’t know whether it covers the fetch gap and --force-if-includes shown above, so treat those as additions from my own run. Philomatics’ shorter “The Dangers of Git Force Push” is linked as a runner-up.

Where this fits

You can publish rewritten history without clobbering anyone. Next, a kind of ref that must never move once published: tags and releases.

Check yourself

  1. 01Why is a normal push rejected after `git commit --amend` on a pushed commit?

    Amending creates a different commit with a new hash. The remote's commit is no longer an ancestor of yours, so the push is not a fast-forward.

  2. 02How is `--force-with-lease` different from `--force`?

    `--force` overwrites unconditionally. The lease only overwrites if the remote branch still points where your `origin/<branch>` says it did, otherwise it fails with `stale info`.

  3. 03You run `git fetch` and then `git push --force-with-lease` without looking at what arrived. Are you protected?

    No. The fetch moved your `origin/<branch>` to the teammate's commit, so the lease matches and the push overwrites it. Look at what you fetched, or add `--force-if-includes`.

  4. 04When is a force push to `main` acceptable?

    Essentially never on a shared branch. Use `git revert` to undo a published change instead. Force pushing is for private or agreed-upon feature branches.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark