Cannot push to GitHub - keeps saying need merge
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Git error "Updates were rejected because the remote contains work that you do not have locally" means the remote branch has commits that your local branch does not have. Git refuses to push because it would overwrite those remote commits. The fix is to pull the remote changes first (with git pull or git pull --rebase), resolve any conflicts, and then push. This happens when someone else pushed to the same branch, or when you made commits directly on GitHub (like editing a README).
The Error Message
This means the commit history has diverged:
Git cannot fast-forward the remote from C to E because D would be lost.
Fix 1: git pull Then Push (Merge)
This creates a merge commit:
Resolving Merge Conflicts
If the same files were changed in both commits, Git reports conflicts:
Fix 2: git pull --rebase (Recommended)
Rebasing replays your local commits on top of the remote changes, creating a linear history:
Result:
Set Rebase as Default
Fix 3: Force Push (Dangerous)
Force push overwrites the remote branch with your local state. Only use this on personal branches where no one else is working:
--force-with-lease is safer because it checks that the remote branch is at the commit you expect. If someone pushed in the meantime, it fails instead of overwriting their work.
Why This Happens
Someone Else Pushed to the Same Branch
Editing Files on GitHub Directly
Force-Pushed from Another Machine
Prevention
Common Pitfalls
- Force-pushing to a shared branch:
git push --forceoverwrites all remote commits, potentially deleting other people's work. Only force-push to branches where you are the sole contributor. Use--force-with-leasefor safety. - Pulling without specifying a strategy: Without
pull.rebaseconfigured,git pulldefaults to merge, creating unnecessary merge commits. Setgit config --global pull.rebase truefor cleaner history. - Not resolving all conflict markers: After a merge conflict, leaving
<<<<<<<,=======, or>>>>>>>markers in the file causes syntax errors. Search for these markers before committing. - Rebasing after already pushing a merge commit: If you already pulled with merge and pushed, rebasing later rewrites the history that others may have. Once pushed, do not rebase those commits.
- Ignoring the error and retrying push repeatedly: The error will not resolve itself. You must integrate the remote changes first with
git pullbefore the push will succeed.
Summary
- The "need merge" error means the remote has commits you do not have locally
- Use
git pull --rebase origin mainto replay your commits on top of remote changes (cleanest approach) - Use
git pull origin mainfor a merge-based integration (creates a merge commit) - Resolve conflicts by editing files, removing conflict markers, staging, and completing the merge/rebase
- Never
git push --forceto shared branches — use--force-with-leaseif force-pushing is truly needed

