Executive Summary & Root Cause Analysis: The error
! [rejected] main -> main (non-fast-forward)followed byerror: failed to push some refs to '...'occurs when you executegit push, but the remote repository contains newer commits that do not exist in your local branch. Git rejects the push by default to prevent you from accidentally overwriting a teammate’s work or losing remote history. Common root causes include: (1) another developer pushed commits to the same branch after your lastgit pull, (2) you initialized the remote repository with aREADME.mdorLICENSEvia the web UI, or (3) you amended (git commit --amend) or rebased commits that had already been pushed to the remote branch.
Step 1: Quick Fix – Fetch and Rebase Before Pushing
The cleanest way to integrate remote changes and replay your local commits on top of the updated remote branch is pulling with --rebase before pushing again:
# 1. Fetch remote updates and replay your local commits on top
git pull --rebase origin main
# 2. If merge conflicts occur, resolve them in your editor, then run:
git add <conflicted-file>
git rebase --continue
# 3. Push your updated commit history to the remote repository
git push origin main
If you prefer a standard merge commit instead of rebasing, run git pull --no-rebase origin main, resolve any conflicts, commit, and push.
Step 2: Handling Rewritten History on Feature Branches (Safe Force Push)
If the rejection happened on your personal feature branch because you intentionally ran git commit --amend or git rebase on already-pushed commits, a standard push will always be rejected as non-fast-forward. Use --force-with-lease instead of --force:
# Safely overwrite the remote feature branch only if no one else pushed new commits
git push --force-with-lease origin <feature-branch>
Why --force-with-lease? Unlike raw git push --force, which blindly destroys any un-fetched commits on the remote, --force-with-lease aborts automatically if the remote branch was updated by someone else since your last git fetch.
Step 3: Alternative Workarounds – Stash, Pull, and Reapply or Branch Off
If your working tree has uncommitted changes blocking git pull, or if the remote branch is protected (such as main with branch protection rules enabled):
- Stash uncommitted work, pull cleanly, and push:
# Save uncommitted local edits temporarily git stash push -m "WIP before sync" # Sync with remote cleanly git pull --rebase origin main # Reapply stashed edits, commit, and push git stash pop git push origin main - Push your commits to a new Pull Request branch (if
mainis protected):# Create a new feature branch from your current local commits git switch -c fix/my-changes # Push the new branch and open a Pull Request git push -u origin fix/my-changes
Verification & Testing Steps
Confirm that your local branch and remote tracking branch share an identical, clean commit history:
# Refresh remote state
git fetch origin
# Verify local branch is up to date with origin
git status -sb
# Inspect the recent commit graph across local and remote refs
git log --oneline --graph --decorate -n 10
Summary Comparison Table
| Root Cause Scenario | Recommended Command | History Impact | Safety Level |
|---|---|---|---|
| Teammate pushed new commits | git pull --rebase origin <branch> && git push |
Linear history (replays local commits on top) | High (Preserves all commits) |
| You amended/rebased a personal PR | git push --force-with-lease origin <branch> |
Rewrites remote branch safely | High (Checks remote state first) |
| Direct push blocked by branch protection | git switch -c <new-branch> && git push -u origin HEAD |
Creates clean PR branch | Highest (No shared branch risk) |
| Uncommitted edits blocking pull | git stash && git pull --rebase && git stash pop |
Integrates remote commits cleanly | High (Protects local WIP) |
Leave a Reply