Git & GitHub Interview Question #140

What does git push --force do and why is it risky?

Security, Team Workflows, Troubleshooting & Senior Git/GitHub Scenarios Senior Advanced

Quick Interview Answer

A force push tells the remote to update a branch even when the update is not a normal fast-forward. It can replace published branch history and make commits used by collaborators disappear from the branch reference.

Detailed Explanation

A force push tells the remote to update a branch even when the update is not a normal fast-forward. It can replace published branch history and make commits used by collaborators disappear from the branch reference.

Force pushing shared branches can disrupt other developers and should normally be restricted by repository rules. It is most commonly encountered after intentionally rewriting a private feature branch.

Code Example

# Dangerous on shared branches:
git push --force origin feature/my-branch

# Prefer safer coordination and protected
# important branches such as main.