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 push --force

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:

# Interactive rebase to edit/squash commits
git rebase -i HEAD~3
# Edit or squash commits, then save and exit the editor

# Remote will reject a normal push due to changed history
git push --force

Example 2 – Amending a commit:

# Fix last commit message or add forgotten changes
git commit --amend -m "Corrected commit message"

# Force push to update remote branch
git push --force

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:

# Suppose we made 4 messy commits on our feature branch
git rebase -i HEAD~4
# Choose 'squash' or 'fixup' in the interactive editor

# Push with force to update cleaned-up history
git push --force

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:

# Go back to a safe commit before the mistake
git reset --hard <last-good-commit-hash>

# Overwrite remote branch with cleaned history
git push --force

We can find <last-good-commit-hash> using:

git log

OR if unsure:

git reflog  # shows a list of recent HEAD states

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?