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
- Make sure you understand the changes you’ve made (e.g. rebased or reset history)
- Stage and commit your changes
- Run:
git push --force
Or for safety:git push --force-with-lease
🔁 Summary Table
Command | Use Case |
---|---|
git push --force | Overwrites remote with local history |
git push --force-with-lease | Safer option that checks remote status |
git rebase -i | Often 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