Git & GitHub
Force Push

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:

BranchWhy Not
main / masterProduction code, everyone depends on it
developIntegration branch, shared by team
Release branchesTagged for deployment
Any shared branchOthers have pulled it

Safe To Force Push:

BranchWhy OK
Your feature branchOnly you use it
Branches with open PRYou own the PR
Clearly marked personal branchesusername/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-name

How It Works

The Difference

CommandChecks RemoteOverwrites 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 GONE

Acceptable 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 fine

2. After Interactive Rebase (Pre-Review)

# Clean up commits before requesting review
git rebase -i main
git push --force-with-lease origin feature/my-work

3. Syncing with Upstream

# Your feature branch is behind
git fetch upstream
git rebase upstream/main
git push --force-with-lease origin feature/my-branch

What 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 undone

For Merging Latest Changes: Merge

# Instead of rebasing shared branch
git checkout main
git merge feature-branch
git push origin main  # Normal push

For Fixing Last Commit (Not Pushed)

# Safe - hasn't been pushed yet
git commit --amend
git push origin branch  # Normal push works

Recovery: 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 branch

If 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/main

Protecting 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 pushes

What 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 1

The 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 ToSafe MethodDangerous Method
Update after rebase--force-with-lease--force
Undo a commitgit revertForce push
Sync with upstreamMerge or rebase + leaseForce push
Fix shared branchRevert commitForce 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-lease and communicate with your team.