KKAIROSافتح التطبيق
Practical guideTested1 min read

Git push rejected: 'fetch first' or 'non-fast-forward' and how to fix it

Published:

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 main

Steps in order

  1. Run git pull --rebase origin main to fetch the remote commits and put yours on top.
  2. If the rebase stops on a conflict: edit the file, git add it, then git rebase --continue.
  3. Push with git push origin main.
  4. Check with git log --oneline that both histories are there.

Common mistakes

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