🔧 Error Fixes
· 1 min read

Git Push Rejected — How to Fix It


! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'origin'
Updates were rejected because the remote contains work that you do not have locally.

Someone else pushed changes to the remote branch since your last pull. Git won’t let you overwrite their work.

Fix 1: Pull Then Push

# ✅ Get the remote changes first
git pull origin main
# Resolve any merge conflicts if needed
git push origin main

Fix 2: Pull with Rebase (Cleaner History)

# ✅ Rebase your commits on top of remote
git pull --rebase origin main
git push origin main

Fix 3: Force Push (Dangerous)

# ⚠️ Only if you know what you're doing
# This OVERWRITES the remote branch
git push --force origin main

# ✅ Safer: force-with-lease (fails if someone else pushed)
git push --force-with-lease origin main

Only force push on your own feature branches, never on shared branches like main.

Fix 4: Branch Protection Rules

# ❌ Branch is protected — can't push directly
# ✅ Create a pull request instead
git checkout -b my-feature
git push origin my-feature
# Then create a PR on GitHub/GitLab

Fix 5: Wrong Remote or Branch

# Check your remotes
git remote -v

# Check which branch you're on
git branch

# Push to the correct remote/branch
git push origin my-branch

Fix 6: Diverged Branches

# ❌ Local and remote have diverged
# ✅ Option A: Merge
git pull origin main  # Creates a merge commit

# ✅ Option B: Rebase (cleaner)
git pull --rebase origin main

# ✅ Option C: Reset to remote (loses local commits!)
git fetch origin
git reset --hard origin/main
📘