git push: error: failed to push some refs (rejected, non-fast-forward)
Most likely cause: Someone else, or you on another machine, pushed to the same branch since you last pulled
Try first: See what is different (git fetch origin && git log --oneline --left-right HEAD...origin/main)
Likely causes
- Someone else, or you on another machine, pushed to the same branch since you last pulledmost likely
- You rewrote local history (rebase, amend, reset) on a branch that was already pushedpossible
- The remote was changed through the web UI (a merged pull request, an edited README, an initial commit when creating the repo)possible
Why this happens
A push is a fast-forward when the remote branch's latest commit is already in your branch's history, so Git only has to add your new commits on top. If the remote has commits you don't have, accepting your push would drop them, so Git rejects it as non-fast-forward.
The fix is always to combine the two histories first. Rebase gives a straight line; merge keeps both lines and adds a merge commit. Both are safe; force-pushing is the only option that discards work.
Common situations
- New GitHub repo created with a README, then git push from an existing project: git pull --rebase origin main, or git pull origin main --allow-unrelated-histories if Git says the histories are unrelated.
- Error after git commit --amend on a pushed commit: your branch now differs from the remote; use --force-with-lease only on your own branch.
- Protected branch: even a force push is refused; open a pull request instead.
How to fix it
See what is differentSafe
Fetch the remote without changing your files, then list commits on each side.
git fetch origin && git log --oneline --left-right HEAD...origin/mainRebase your commits on top of the remoteChanges settings
Replays your local commits after the remote ones, keeping history linear. Replace main with your branch name.
git pull --rebase origin mainOr merge insteadChanges settings
If your team prefers merge commits, or you're unsure about rebasing, a normal pull creates a merge commit.
git pull origin mainResolve any conflictsChanges settings
Edit the conflicted files, then mark them resolved. During a rebase continue with git rebase --continue; to back out, use git rebase --abort.
git add <file> && git rebase --continuePush againChanges settings
Once your branch contains the remote commits, the push is a fast-forward and goes through.
git push origin mainOnly if you rewrote your own branch: force with leaseDangerous
If the branch is yours alone and you intentionally amended or rebased it, push with --force-with-lease, which refuses if someone else pushed in the meantime.
git push --force-with-lease origin my-branch⚠ Make a backup branch first (git branch backup-my-branch). Never force-push main or any shared branch; it can delete other people's commits from the remote.
If that didn't fix it
If the push is still rejected after pulling, check the full message: 'protected branch' or 'pre-receive hook declined' are server rules, not history problems. Related: [fatal: refusing to merge unrelated histories](/code/fatal-refusing-to-merge-unrelated-histories).
When to see a technician
If a rebase goes wrong, run git rebase --abort to get back to where you started, or git reflog to find your previous commit, and ask a teammate before forcing anything.
Questions people ask
- Should I use git push --force?
- Not on shared branches. Use git pull --rebase or a merge. On a branch only you use, prefer --force-with-lease over --force.
- Rebase or merge?
- Follow your team's convention. Rebase keeps history linear but rewrites your unpushed commits; merge never rewrites but adds a merge commit. Either fixes this error.