Force Push
About
A force push in Git (git push --force
) is a command used to push our local branch to the remote repository while overwriting the remote branch history. It disregards the normal safety checks that prevent rewriting commits that exist on the remote.
Normally, Git protects shared history. If our local branch has diverged from the remote (i.e., the histories are different), a regular push will be rejected. Force push bypasses that rejection.
Why Force Push Exists
Git allows rewriting history locally using commands like:
git rebase
git commit --amend
git reset
After such operations, our branch’s history no longer matches the remote. If we try to push, Git will refuse to do so unless we use force. This is where force push comes in — it lets us tell Git, “I know what I’m doing, replace the remote history with mine.”
How It Works
When we run:
Git does not care whether the remote branch has new commits. It simply updates the remote branch to point to our current local state. If the remote had commits not present locally, they are lost.
Git compares commit references (pointers) and updates them directly.
When to Use Force Push ?
a) After Rewriting History
Scenario: We have rewritten commit history using git rebase
or git commit --amend
.
Why: These operations change commit hashes, making your local branch history different from the remote. A normal git push
will be rejected.
Example 1 – Rebase:
Example 2 – Amending a commit:
Note: Always ensure no one else is working on the same branch before you force push.
b) Cleanup Before Merge Request
Scenario: We want to clean up messy commits before raising a Merge Request or Pull Request.
Why: To present a clear and linear commit history, we may squash multiple WIP (work in progress) commits into one or reorder them.
Example – Squashing commits:
This is commonly done just before opening a PR/MR, or after feedback, so the reviewer sees a clean diff.
c) Removing Mistaken Commits
Scenario: We accidentally pushed something sensitive (e.g., secrets, large files, wrong code).
Why: We want to remove the commit(s) from remote history so it’s no longer publicly visible or downloadable.
Example – Using git reset
:
We can find <last-good-commit-hash>
using:
OR if unsure:
Use this carefully. If others pulled the bad commit, removing it locally won't remove it from their machines.
Risks and Why Force Push is Dangerous
a) Data Loss
Force push removes remote commits that are not in our local history. If someone else pushed to the same branch before our force push, their work is lost.
b) Team Disruption
If teammates have pulled the old version and we force push a new history:
They get errors when pulling.
They must fix their local branches manually using rebase or reset.
c) Broken CI/CD Pipelines
Force pushing can break automated pipelines that rely on commit history, hashes, or tags.
When to Use and When NOT to Use Force Push
Situation / Scenario
Use --force
?
Explanation
After interactive rebase (git rebase -i
)
Yes
Rebase rewrites history; force push is required to update the remote.
After amending the latest commit (--amend
)
Yes
Changes the commit hash; remote needs a force push to accept the new history.
After squashing commits for clean history
Yes
Squash modifies commit history; you must force push to update the branch on the remote.
After resetting to remove sensitive/mistaken data
Yes
You are rewriting history to remove bad commits; force push is required.
Working on your own feature branch alone
Yes
Safe to force push if no one else depends on the branch.
Collaborating on a shared branch with others
No
Force push can overwrite others’ work; use with extreme caution or avoid it.
Trying to sync local with updated remote
No
Use git pull
, git fetch
, or merge instead; force push can erase incoming updates.
Undoing a mistake without rewriting history
No
Use git revert
to add a new commit that undoes the changes.
On protected branches (e.g., main
, release
)
No
Force push is typically disabled or discouraged to avoid accidental overwrites.
As a shortcut to fix conflicts
No
Don't use force push to avoid resolving conflicts. Always handle them properly.
Accidentally diverged due to git pull
conflicts
Maybe
Understand what caused divergence. Use --force-with-lease
if safe, but only if you're sure.
Last updated
Was this helpful?