Push rejected (non-fast-forward / fetch first)
careful beginner #push#rejected#non-fast-forward#force
When to use this: git push fails with rejected because GitHub has commits you do not have.
Be careful with this one. Do not fix this with
--force. That overwrites GitHub and can erase other work. Before you start
Steps
-
See the difference
terminalgit fetch git status -sbbehind Nmeans GitHub has N commits you lack.aheadandbehindtogether means diverged. -
Combine GitHub's commits with yours
terminalgit pull --no-rebaseMerges the remote changes into your branch. Resolve any conflicts (see the conflict scenario).
-
Push again
terminalgit pushNow your branch contains everything on GitHub, so the push is a fast-forward.
How to verify it worked
git pushfinishes withmain -> main, andgit statussays up to date.
How to undo it
A pull that went wrong and is not pushed: git reset --hard ORIG_HEAD.
Common errors and fixes
You really did rewrite history (amend or rebase) and want to overwrite your own branch Use
git push --force-with-lease and only on your own feature branch, never on main. failed to push some refs ... hint: Updates were rejected because the tip of your current branch is behind Same as above: pull first, then push. Q&A
Why does Git refuse my push?
A push must only move the remote branch forward along your history. If GitHub has commits you lack, accepting the push would erase them.
What is the difference between --force and --force-with-lease?
--force overwrites no matter what. --force-with-lease aborts if the remote has commits you have not seen, so it will not silently destroy someone else's work.