Practical guideTested1 min read
Git push rejected: 'fetch first' or 'non-fast-forward' and how to fix it
Advertisement
What the message means
The remote branch has commits you do not have yet, so Git refuses your push to avoid overwriting them.
Some Git versions print non-fast-forward instead; the cause and the fix are the same.
Fix
git pull --rebase origin main
git push origin mainSteps in order
- Run
git pull --rebase origin mainto fetch the remote commits and put yours on top. - If the rebase stops on a conflict: edit the file,
git addit, thengit rebase --continue. - Push with
git push origin main. - Check with
git log --onelinethat both histories are there.
Common mistakes
- Using
git push --forceon a shared branch. It can erase the other person's commits. If you must force,--force-with-leaserefuses when the remote moved since you last fetched. - Pulling without looking at the result. Run
git log --onelineafter the pull to confirm both histories are there.
Should I use git pull or git pull --rebase?
Both bring the remote commits in. pull makes a merge commit; --rebase replays your commits on top, which keeps the log in one line.
What if the rebase stops with a conflict?
Fix the file, git add it, then git rebase --continue. git rebase --abort returns you to before the pull.
Source: https://git-scm.com/docs/git-push and https://git-scm.com/docs/git-pull
Full kit: Git Survival Kit