How to Force Push in Git (And When You Should or Shouldn’t)

In Git, force pushing allows you to overwrite the remote history with your local changes. While powerful, it’s also risky—so understanding when and how to use it properly is crucial.


🛠️ What Is Force Push?

A force push is used when your local branch’s history diverges from the remote branch’s history and you want to overwrite the remote branch with your local version.

The command:

git push --force

or shorthand:

git push -f

⚠️ When to Use Force Push

You might need to force push in these scenarios:

  • You rebase your branch and need to push the rewritten history
  • You accidentally committed sensitive data and need to rewrite history
  • You need to undo commits or reset your branch to a previous state

🧪 Example: Force Pushing After Rebase

Let’s say you rebased your feature branch to clean up commit history:

git rebase -i main

Then push:

git push --force

This is necessary because the rebase rewrites commit hashes, making your local branch incompatible with the remote one.


✅ Safer Alternative: Force Push With Lease

To avoid accidentally overwriting someone else’s work, use:

git push --force-with-lease

This only force-pushes if no one else has pushed to the remote branch since you last pulled. It’s safer than a raw --force.


❌ When Not to Force Push

Avoid using --force on shared branches like main or develop. It can:

  • Delete commits pushed by other collaborators
  • Cause confusion and broken histories
  • Trigger unnecessary bugs or rollback efforts

👨‍💻 How to Force Push Step-by-Step

  1. Make sure you understand the changes you’ve made (e.g. rebased or reset history)
  2. Stage and commit your changes
  3. Run: git push --force Or for safety: git push --force-with-lease

🔁 Summary Table

CommandUse Case
git push --forceOverwrites remote with local history
git push --force-with-leaseSafer option that checks remote status
git rebase -iOften followed by a force push

🧠 Pro Tips

  • Communicate with your team before force pushing
  • Never force push to shared or protected branches
  • Use --force-with-lease as a safer default
  • Protect important branches on GitHub to prevent force pushes
Sharing Is Caring:

Leave a Comment