What's Actually Happening

A force push (git push --force) overwrites the remote branch history with your local history. If you force pushed the wrong branch or wrong commits, you've overwritten other people's work on the remote. Recovery requires finding the lost commits and restoring them.

The Error Scenario

```bash # Accidentally ran this on wrong branch git push --force origin main

# Or pushed wrong commits git reset --hard HEAD~5 git push --force origin main ```

Now remote main points to your local state, overwriting commits others made.

Why This Happens

  1. 1.Wrong branch - Thought you were on feature branch, actually on main
  2. 2.Wrong terminal/machine - Pushed from outdated clone
  3. 3.Rebase cleanup went too far - Reset more than intended before force push
  4. 4.Automation mistake - CI/CD script force pushed unexpectedly
  5. 5.Missing communication - Didn't check if others pushed since your last pull

Step 1: Stop and Don't Push Anything Else

Critical: Do not make any more pushes until recovery is complete. Additional pushes can make recovery harder.

Step 2: Find Lost Commits on Remote

If you have access to the remote machine (server):

```bash # On the Git server, check reflog cd /path/to/repo.git git reflog show main

# Find commits before force push # Example output: # abc123d main@{0}: update by push: forced-update # def4567 main@{1}: update by push: Last good commit # ghi890j main@{2}: update by push: Another lost commit ```

Step 3: Recover Using GitHub/GitLab

If using GitHub:

  1. 1.Go to repository on GitHub
  2. 2.Check commit history - may still show lost commits briefly
  3. 3.Use GitHub API to find commits:

```bash # Get all events including force pushes gh api repos/user/repo/events

# Find push event before force push gh api repos/user/repo/commits ```

On GitHub, commits may still exist even after force push (they're just not on any branch). Find the SHA and create branch.

  1. 1.For GitLab:
  2. 2.Check Repository > Commits
  3. 3.Look at recent activity events
  4. 4.Commits may still be accessible via direct SHA URL

Step 4: Ask Team Members for Help

The most reliable recovery: ask teammates who pulled recently.

```bash # Teammate who pulled before force push git fetch origin git log origin/main --oneline -10

# Their origin/main still has the lost commits # Ask them to create a branch and push: git branch recovery origin/main git push origin recovery ```

Now you have a recovery branch with all lost commits.

Step 5: Restore from Teammate's Branch

```bash # Fetch the recovery branch git fetch origin recovery

# Verify it has lost commits git log origin/recovery --oneline

# Reset your main to match git checkout main git reset --hard origin/recovery

# Force push to restore git push --force origin main ```

Step 6: Use Local Reflog on Your Machine

If you pulled the commits before force pushing:

```bash git reflog show origin/main

# Find entries before your force push # Example: # abc123d origin/main@{0}: update by push (forced update) # def4567 origin/main@{1}: fetch: before force push ```

Create branch from old origin/main:

bash
git branch origin-recovery origin/main@{1}

Step 7: Recover if You Were on Wrong Branch

If you force pushed wrong branch content:

```bash # Check what you pushed git log main --oneline -5

# Find where main should be (from reflog or teammate) git reflog show main

# Reset to correct state git reset --hard HEAD@{n}

# Push correct history git push --force origin main ```

Step 8: Communicate with Team

After force push accident:

  1. 1.Notify team immediately - "I accidentally force pushed main"
  2. 2.Ask everyone to stop work on that branch
  3. 3.Coordinate recovery - get someone with latest commits to help
  4. 4.Verify everyone re-fetches after fix
bash
# Everyone should run after fix
git fetch origin
git reset --hard origin/main

Step 9: Check for Orphaned Commits on GitHub

GitHub keeps orphaned commits for a period. Try direct URL:

bash
https://github.com/user/repo/commit/abc123def456...

If commit still exists (not garbage collected): - Create branch pointing to it - Push to restore

bash
git fetch origin abc123def456...
git checkout -b recovery abc123def456...
git push origin recovery

Verify the Fix

After recovery:

```bash # Check main has correct commits git log origin/main --oneline -10

# Compare with before force push (if you have reference) git log origin/main --oneline | grep "commit from teammate"

# Verify branch is restored git fetch origin git log origin/main ```

Prevention Tips

**Never use --force by default**. Use safer alternatives:

```bash # Use --force-with-lease instead git push --force-with-lease origin main

# This checks if remote has commits you don't know about # Safer than blind --force ```

Before force push:

```bash # Always check remote state first git fetch origin git log HEAD..origin/main --oneline

# If there are commits, DO NOT force push # Merge/rebase first, then push normally ```