When Not to Force Push
Force push is a destructive operation. It overwrites history. One wrong force push can destroy your teammates' work.
What Force Push Does
Commit C is gone. Anyone who had C will have problems.
The Danger Zone
Never Force Push To:
| Branch | Why Not |
|---|---|
main / master | Production code, everyone depends on it |
develop | Integration branch, shared by team |
| Release branches | Tagged for deployment |
| Any shared branch | Others have pulled it |
Safe To Force Push:
| Branch | Why OK |
|---|---|
| Your feature branch | Only you use it |
| Branches with open PR | You own the PR |
| Clearly marked personal branches | username/feature pattern |
Decision Flowchart
Safe Force Push: --force-with-lease
Always use --force-with-lease instead of --force:
# β Dangerous - overwrites blindly
git push --force origin branch-name
# β
Safe - checks if remote changed
git push --force-with-lease origin branch-nameHow It Works
The Difference
| Command | Checks Remote | Overwrites Others' Work |
|---|---|---|
--force | β No | β οΈ Yes, always |
--force-with-lease | β Yes | β No, fails if changed |
Scenarios Where Force Push Causes Problems
Scenario 1: Overwriting Teammate's Commit
You force push, overwriting C:
Teammate: "Where did my commit go?!"Scenario 2: Breaking Everyone's Local
Scenario 3: Lost Work
# You have: A -- B -- C
# Remote has: A -- B -- C -- D (colleague's work)
# You rebase: A -- B -- C' (rewritten)
# You force push
# Result: D is GONEAcceptable Force Push Scenarios
1. Your Own Feature Branch (PR Open)
# Reviewer asks for changes, you squash
git rebase -i HEAD~3
git push --force-with-lease origin feature/your-branch
# This updates your PR - perfectly fine2. After Interactive Rebase (Pre-Review)
# Clean up commits before requesting review
git rebase -i main
git push --force-with-lease origin feature/my-work3. Syncing with Upstream
# Your feature branch is behind
git fetch upstream
git rebase upstream/main
git push --force-with-lease origin feature/my-branchWhat To Do Instead of Force Push
For Shared Branches: Revert
# Instead of rewriting history, add a revert commit
git revert abc123
git push origin main # Normal push
# History is preserved, mistake is undoneFor Merging Latest Changes: Merge
# Instead of rebasing shared branch
git checkout main
git merge feature-branch
git push origin main # Normal pushFor Fixing Last Commit (Not Pushed)
# Safe - hasn't been pushed yet
git commit --amend
git push origin branch # Normal push worksRecovery: After Bad Force Push
If You Force Pushed Your Branch
# Find the old commit
git reflog
# Output:
# abc123 HEAD@{0}: push --force
# def456 HEAD@{1}: rebase
# ghi789 HEAD@{2}: commit (before rebase)
# Reset to old state
git reset --hard ghi789
git push --force-with-lease origin branchIf Someone Force Pushed Shared Branch
# Fetch latest
git fetch origin
# Your local diverged
git status
# Your branch and 'origin/main' have diverged
# Option 1: Accept their version
git reset --hard origin/main
# Option 2: Keep your commits
git rebase origin/mainProtecting Branches
GitHub Branch Protection
Maintainers can prevent force push:
Repository Settings β Branches β Add Rule
- Branch name pattern: main
- β
Require pull request before merging
- β
Do not allow force pushesWhat It Looks Like
git push --force origin main
# ! [remote rejected] main -> main (protected branch hook declined)Git Aliases for Safety
Add to ~/.gitconfig:
[alias]
# Make force-with-lease the default
pushf = push --force-with-lease
# Remind yourself of the danger
forcepush = !echo 'Are you sure? Use pushf instead' && exit 1The Force Push Checklist
Before force pushing, verify:
- This is MY branch (not shared)
- OR everyone affected has been notified
- I'm using
--force-with-lease - I've fetched recent changes first
- I have the reflog if I need to recover
- This is NOT main/master/develop
Quick Reference
| Want To | Safe Method | Dangerous Method |
|---|---|---|
| Update after rebase | --force-with-lease | --force |
| Undo a commit | git revert | Force push |
| Sync with upstream | Merge or rebase + lease | Force push |
| Fix shared branch | Revert commit | Force push |
| Fix own PR | --force-with-lease | --force |
Communication Template
If you MUST force push a shared branch:
β οΈ FORCE PUSH ALERT β οΈ
I need to force push to branch: feature/shared-work
Reason: [explain why]
Time: [when you'll do it]
Before I push:
1. Push any uncommitted changes
2. Back up your local branch: git branch backup-$(date +%Y%m%d)
After I push:
1. Run: git fetch origin
2. Run: git reset --hard origin/feature/shared-work
Questions? Reply before [time].Summary
Section Complete! π
You've completed the Git & GitHub Essentials section. You now understand:
- β Git basics
- β Branching strategies
- β Commit messages
- β Fork syncing
- β Conflict resolution
- β History rewriting
- β Safe force pushing
Next Section
Ready to make your first contribution?
β‘οΈ Making Your First Contribution β
Final Thought: Force push is like a power toolβextremely useful in the right hands, dangerous in the wrong ones. Default to
--force-with-leaseand communicate with your team.