How to push force with lease in Git

Git push –force-with-lease is a safer alternative to –force that prevents overwriting commits you haven’t seen, protecting against accidentally destroying teammates’ work. As the creator of CoreUI with 26 years of development experience, I’ve used force-with-lease across hundreds of repositories to safely rewrite history while maintaining team collaboration, preventing countless instances of lost work.

The most reliable approach uses –force-with-lease instead of –force for all force push operations.

Basic Force with Lease

# Regular force push (dangerous)
git push --force origin feature-branch

# Safer force push
git push --force-with-lease origin feature-branch

How It Works

# Your local branch
git log --oneline
# abc123 Latest commit
# def456 Previous commit

# Remote has this
# Remote: abc123 Latest commit
# Remote: def456 Previous commit

# You rebase
git rebase main
# New commits: xyz789, abc123 (rewritten)

# Force with lease checks remote state
git push --force-with-lease origin feature-branch
# ✓ Success - remote matches expected state

# But if someone else pushed
# Remote: ghi789 (new commit by teammate)
# Remote: abc123
# Remote: def456

git push --force-with-lease origin feature-branch
# ✗ Failed - remote has changed!
# error: failed to push some refs

Compare with Regular Force

# Regular --force (dangerous)
git push --force origin feature-branch
# Overwrites remote regardless of state
# Can destroy teammates' work

# --force-with-lease (safer)
git push --force-with-lease origin feature-branch
# Only pushes if remote matches your last fetch
# Protects teammates' work

Specify Expected Remote State

# Force with lease for specific commit
git push --force-with-lease=feature-branch:abc1234 origin feature-branch

# Only succeeds if remote branch is at commit abc1234

After Rebase Workflow

# Fetch latest changes
git fetch origin

# Rebase your branch
git checkout feature-branch
git rebase origin/main

# Resolve conflicts if any
git add .
git rebase --continue

# Safe force push
git push --force-with-lease origin feature-branch

Interactive Rebase Example

# Start interactive rebase
git rebase -i HEAD~3

# Squash or reword commits
# Save and exit

# Push with lease
git push --force-with-lease origin feature-branch

# If remote changed during rebase
# Fetch first
git fetch origin

# Then try again
git push --force-with-lease origin feature-branch

Configure Default Behavior

# Make --force-with-lease the default for this repo
git config push.useForceIfIncludes true

# Now regular push after rebase uses force-with-lease
git push

# Configure globally
git config --global push.useForceIfIncludes true

Alias for Safety

# Create safer force push alias
git config --global alias.pushf 'push --force-with-lease'

# Usage
git pushf origin feature-branch

Handle Rejection

# Force with lease rejected
git push --force-with-lease origin feature-branch
# error: failed to push some refs

# Fetch to see what changed
git fetch origin

# View differences
git log feature-branch..origin/feature-branch

# If changes are from teammate, coordinate
# If safe to overwrite, fetch and force push
git fetch origin
git push --force-with-lease origin feature-branch

# Or rebase on top of remote changes
git rebase origin/feature-branch
git push

Pre-push Hook

Create .git/hooks/pre-push:

#!/bin/bash

# Prevent --force without --force-with-lease
while read local_ref local_sha remote_ref remote_sha
do
  # Check if this is a force push
  if [ "$remote_sha" = "0000000000000000000000000000000000000000" ]
  then
    echo "❌ Force push detected without --force-with-lease"
    echo "Use: git push --force-with-lease"
    exit 1
  fi
done

exit 0

Make executable:

chmod +x .git/hooks/pre-push

Team Workflow

# Developer A working on feature
git checkout -b feature-new-ui
# ... make commits ...
git push origin feature-new-ui

# Developer B also working on same branch
git checkout feature-new-ui
# ... make commits ...
git push origin feature-new-ui

# Developer A rebases
git fetch origin
git rebase origin/main

# Try to force push
git push --force-with-lease origin feature-new-ui
# ✗ Failed - Developer B pushed new commits

# Developer A fetches and rebases
git fetch origin
git rebase origin/feature-new-ui

# Now force with lease succeeds
git push --force-with-lease origin feature-new-ui

When to Use

# ✓ Good use cases:
# 1. After rebasing
git rebase main
git push --force-with-lease

# 2. After amending last commit
git commit --amend
git push --force-with-lease

# 3. After interactive rebase
git rebase -i HEAD~5
git push --force-with-lease

# 4. After cleaning history
git filter-branch
git push --force-with-lease

# ✗ Bad use cases:
# Don't force push to shared branches like main/master
# Don't force push to public repositories others depend on

Best Practice Note

This is the force push strategy we use across all CoreUI repositories for safe history rewriting. Always use –force-with-lease instead of –force to prevent accidentally overwriting teammates’ work. The lease mechanism checks if the remote branch matches your last fetch, failing if someone else pushed in the meantime. Configure aliases or default behavior to make force-with-lease the standard, and never force push to protected branches like main or master.

For related Git operations, check out how to push specific branch in Git and how to rebase in Git safely.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author