Nutshell Series

Git Force Push: Do’s & Don’ts (Quick Guide)

Force push (git push --force) overwrites the remote branch to match your local branch, even if it rewrites commit history.

✅ Do

  • Prefer --force-with-lease (safer than --force):
git fetch origin
git push --force-with-lease origin my-branch
  • Force-push only your feature branches (e.g., feature/*, bugfix/*).
  • Create a backup branch/tag first (easy rollback):
git fetch origin
git branch backup/my-branch origin/my-branch
# or: git tag backup-my-branch origin/my-branch
  • Warn teammates if anyone else might be using the branch.

❌ Don’t

  • Don’t force-push to main/master or release/* (breaks traceability and can disrupt CI/CD).
  • Don’t use --force blindly (can overwrite someone else’s commits).
  • Don’t rewrite shared history after others have pulled and started work.
  • Don’t “fix conflicts” by deleting remote work via force push.

Safe Pattern (Copy/Paste)

git checkout my-branch
git fetch origin
git branch backup/my-branch origin/my-branch
git rebase origin/main
git push --force-with-lease origin my-branch

Rule of thumb: If the branch is shared or protected, avoid force push. If it’s your feature branch, use --force-with-lease and back it up first.

Leave a comment