Force Push Safely
Two people edit the same shared document offline. One of them reconnects and clicks "Replace the online version with mine". The other person's changes, made in the meantime, are simply gone from the shared copy.
That button is git push --force. Normally, the server only accepts a push that adds commits, as you saw in "Git Push". A force push tells it to accept anything, even if that throws away commits on the server. There are good reasons to force push, mostly after you rewrite the history of your own branch with --amend or rebase. But a careless force push is the most common way people lose a teammate's work. Git has two safer options that check before overwriting, and you should use them every time.
In this lesson, you will see why a rewritten branch needs a force push, watch a plain --force overwrite a teammate's commit, recover it, and use the safe options instead.
The Starting Point
Navika pushed feature/receipts with one commit. Advitha picked it up, added a commit and pushed:
git init --bare --quiet fee-tracker.git
mkdir navika
cd navika
git init --quiet fee-tracker
cd fee-tracker
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbrs"
git push --quiet -u origin feature/receipts
cd ../..
git clone --quiet fee-tracker.git advitha/fee-tracker
cd advitha/fee-tracker
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
git switch --quiet feature/receipts
echo "Print after payment" >> receipts.txt
git commit --quiet -am "Print a receipt after payment"
git push --quiet
git log --onelinee73fc67 (HEAD -> feature/receipts, origin/feature/receipts) Print a receipt after payment
1e09bdd Add receipt numbrs
57fe1a7 (origin/main, origin/HEAD, main) Add project READMEWhy a Rewritten Branch Needs Force
Navika notices the spelling mistake in her commit message, "numbrs". She hasn't fetched, so she doesn't know about Advitha's commit. She fixes the message with --amend and pushes:
cd ../../navika/fee-tracker
git commit --quiet --amend -m "Add receipt numbers"
git pushTo ../../fee-tracker.git
! [rejected] feature/receipts -> feature/receipts (fetch first)
error: failed to push some refs to '../../fee-tracker.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.Rejected, as expected. But think about what would happen even without Advitha's commit. --amend replaced Navika's commit with a new one with a different ID. The server's branch has the old commit, and her branch has the new one instead. Her branch no longer contains the server's commit, so the push isn't a fast-forward, and only a force push could replace it. That is the legitimate use: after rewriting commits that you already pushed to your own branch.
The Safe Force Push: --force-with-lease
--force-with-lease overwrites the server's branch only if it is still where your origin/feature/receipts says it is, that is, only if nobody has pushed since you last fetched:
git push --force-with-leaseTo ../../fee-tracker.git
! [rejected] feature/receipts -> feature/receipts (stale info)
error: failed to push some refs to '../../fee-tracker.git'Rejected again, with (stale info). The server's branch has moved since Navika last fetched: Advitha's commit is there. The lease protected it. Without it, here is what happens.
What Plain --force Does
Navika uses plain --force, as many tutorials still suggest:
git push --forceTo ../../fee-tracker.git
+ e73fc67...6704947 feature/receipts -> feature/receipts (forced update)+ ... (forced update) means the server accepted a branch that isn't a fast-forward. Advitha's commit is no longer on feature/receipts on the server. When Advitha fetches, she sees the same forced update:
cd ../../advitha/fee-tracker
git fetchFrom /home/navika/projects/fee-tracker
+ e73fc67...6704947 feature/receipts -> origin/feature/receipts (forced update)git status | head -3On branch feature/receipts
Your branch and 'origin/feature/receipts' have diverged,
and have 2 and 1 different commits each, respectively.Recover the Overwritten Commit
The commit isn't lost, because Advitha still has it on her computer. She replays it on top of the new branch, and pushes it back:
git rebase --no-fork-point origin/feature/receiptswarning: skipped previously applied commit 1e09bdd
hint: use --reapply-cherry-picks to include skipped commits
hint: Disable this message with "git config set advice.skippedCherryPicks false"
Successfully rebased and updated refs/heads/feature/receipts.git push --quiet
git log --oneline2066ef3 (HEAD -> feature/receipts, origin/feature/receipts) Print a receipt after payment
6704947 Add receipt numbers
57fe1a7 (origin/main, origin/HEAD, main) Add project READMEThe rebase replayed Advitha's commit on top of Navika's amended one, and dropped Navika's old commit, because its change was already there. The branch has both people's work again.
Note the --no-fork-point. A plain git pull --rebase would have dropped Advitha's commit here. Advitha had pushed that commit herself, so her origin/feature/receipts once pointed to it. By default, a rebasing pull uses that history to guess which commits came from the server, decides her commit belonged to the server's branch and was removed on purpose, and leaves it out. --no-fork-point turns that guess off and replays everything since the real common ancestor. If nobody had the commit locally, git reflog on the computer that last pushed it, or the hosting platform's activity log, is where to look.
The Gap --force-with-lease Leaves: --force-if-includes
--force-with-lease compares the server with your origin/... copy. But that copy is updated by every fetch, including one your IDE runs in the background. After a fetch, the lease looks fresh, even if you never looked at the new commits.
Here, Navika pulls Advitha's work, adds a footer and pushes. Advitha then pushes one more commit. Navika fetches (without merging), and amends her footer commit:
cd ../../navika/fee-tracker
git pull --quiet --rebase
echo "Thank you for your payment" >> receipts.txt
git commit --quiet -am "Add a receipt footr"
git push --quiet
cd ../../advitha/fee-tracker
git pull --quiet --rebase
echo "School: Anamaneni Public School" >> receipts.txt
git commit --quiet -am "Add the school name"
git push --quiet
cd ../../navika/fee-tracker
git fetch --quiet
git commit --quiet --amend -m "Add a receipt footer"
git push --force-with-lease --force-if-includesTo ../../fee-tracker.git
! [rejected] feature/receipts -> feature/receipts (remote ref updated since checkout)
error: failed to push some refs to '../../fee-tracker.git'
hint: Updates were rejected because the tip of the remote-tracking branch has
hint: been updated since the last checkout. If you want to integrate the
hint: remote changes, use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.--force-with-lease alone would have passed, because the fetch made origin/feature/receipts match the server, and Advitha's "Add the school name" would have been overwritten. --force-if-includes (Git 2.30 and later) adds a second check: the server's commit must be part of your branch's history, meaning you actually integrated it, not just fetched it. Navika hadn't, so the push is refused.
The fix is the same as for any rejected push: integrate first. Here, she rebases her branch onto the server's:
git rebase origin/feature/receiptswarning: skipped previously applied commit 9809589
hint: use --reapply-cherry-picks to include skipped commits
hint: Disable this message with "git config set advice.skippedCherryPicks false"
Successfully rebased and updated refs/heads/feature/receipts.git log --oneline -375c13eb (HEAD -> feature/receipts, origin/feature/receipts) Add the school name
f53e313 Add a receipt footr
2066ef3 Print a receipt after paymentThe rebase found that her footer commit's change was already on the server, in the old commit with the misspelled message, and dropped it as a duplicate. The branch now matches the server, with Advitha's newest commit included. To fix the old message as well, Navika would now amend in agreement with Advitha, or simply leave it: a misspelled message isn't worth rewriting a shared branch.
Syntax
| Command | It is used to |
|---|---|
git push --force-with-lease | Force push only if the server branch is where you last saw it |
git push --force-with-lease --force-if-includes | Also require that you integrated what you last saw |
git push --force-with-lease=<branch>:<commit> | Force push only if the server branch is at an exact commit |
git push --force or -f | Overwrite the server branch unconditionally (avoid) |
git config --global push.useForceIfIncludes true | Always add --force-if-includes to --force-with-lease |
When to Force Push, and When Not
| Situation | Force push? |
|---|---|
| You amended or rebased commits on your own feature branch, already pushed | Yes, with --force-with-lease |
| You squashed commits on your pull request branch before review | Yes, with --force-with-lease |
| A push to a shared branch is rejected | No. Pull and push again |
main, develop or any branch others commit to | No. Use git revert to undo |
| You removed a secret from history | Yes, coordinated with the team, and change the secret anyway |
Hosting platforms let administrators protect branches such as main: force pushes to them are refused by the server, whatever options you use.
Common Mistakes
Force pushing to get past a rejected push. The rejection means the server has commits you don't. Pull them.
Using --force out of habit. Make --force-with-lease the only force you type. Some people add an alias, such as git config --global alias.pushf "push --force-with-lease --force-if-includes".
Force pushing main. Even with a lease, rewriting a shared branch breaks everyone's local copy. Revert instead.
Rewriting a branch someone else is working on without telling them. Even a safe force push surprises a teammate whose next pull has to rebase. Agree first.
Thinking a forced-over commit is gone forever. It still exists in the repository of anyone who had it, and in their reflog. Recover it from there.
Under the Hood
A normal push asks the server to move a branch from commit A to commit B, and the server checks that A is an ancestor of B. --force skips that check. --force-with-lease sends an extra condition: "only if the branch is still at A", where A is your origin/... value. The server compares and refuses if the branch has moved, which is the (stale info) you saw.
--force-if-includes is checked on your side, before anything is sent. Git looks at the reflog of your remote-tracking branch, and checks that the commit the lease uses appears somewhere in the reflog of your local branch, meaning your branch was based on it at some point. A background fetch updates origin/... without your branch ever containing the new commit, so the check fails.
Interview Corner
When do you need to force push?
After rewriting commits that are already on the remote branch, for example with git commit --amend, git rebase or a squash. The rewritten branch no longer contains the remote's commits, so a normal push is rejected as a non-fast-forward.
What is the difference between --force and --force-with-lease?
--force overwrites the remote branch unconditionally, which can delete commits others pushed. --force-with-lease overwrites only if the remote branch is still at the commit your remote-tracking branch records, so it refuses if someone pushed since your last fetch.
What does --force-if-includes add?
It makes --force-with-lease also require that the remote commit is part of your local branch's history (checked through the reflog). That closes the gap where a background fetch updates origin/..., making the lease pass even though you never integrated the new commits.
Someone force pushed and your commits disappeared from the remote branch. What do you do?
Your local branch still has them. Fetch, then rebase onto the new remote branch with git rebase --no-fork-point origin/<branch> to replay your commits on top, and push. A plain git pull --rebase can skip commits you had pushed yourself, because its fork-point guess treats them as removed upstream. If your local copy is gone too, find the commits with git reflog.
Should you ever force push to main?
Generally no. main is shared, and rewriting it disrupts everyone. Use git revert to undo changes there. Most teams protect main so the server refuses force pushes.
Practice
Challenge: fix your own branch. In Navika's clone, create fix/rounding, commit Round to the nearest dollar in a file rounding.txt with the message "Round fees", and push it with -u. Nobody else uses this branch. Then amend the message to "Round fees to the nearest dollar" and push with --force-with-lease --force-if-includes.
What does Git print? Before you open the solution, decide whether the force push is accepted, and what Git reports for it.
Show solution
git switch --quiet main
git switch --quiet -c fix/rounding
echo "Round to the nearest dollar" > rounding.txt
git add rounding.txt
git commit --quiet -m "Round fees"
git push --quiet -u origin fix/rounding
git commit --quiet --amend -m "Round fees to the nearest dollar"
git push --force-with-lease --force-if-includesTo ../../fee-tracker.git
+ 7284b5d...393ca4f fix/rounding -> fix/rounding (forced update)Accepted, with (forced update). Nobody else had pushed to fix/rounding, so the server was exactly where Navika's origin/fix/rounding said, and she had pushed that commit herself, so both checks passed. This is the normal, safe use of a force push.
Checkpoint. With two clones of one repository, push a branch from the first and add a commit to it from the second. In the first, amend and try --force-with-lease: see (stale info). Then fetch and try --force-with-lease --force-if-includes: see it refuse. Finally, integrate, and push without force.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git push --force-with-lease --force-if-includes | Force push safely |
git config --global push.useForceIfIncludes true | Always add the second check |
git config --global alias.pushf "push --force-with-lease --force-if-includes" | A short safe force push |
git rebase --no-fork-point origin/<branch> | Recover your commits after someone force pushed |
git reflog | Find commits that were overwritten |
git revert <commit> | Undo on a shared branch instead of forcing |
Was this tutorial helpful?