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

  1. See the difference

    terminal
    git fetch
    git status -sb

    behind N means GitHub has N commits you lack. ahead and behind together means diverged.

  2. Combine GitHub's commits with yours

    terminal
    git pull --no-rebase

    Merges the remote changes into your branch. Resolve any conflicts (see the conflict scenario).

  3. Push again

    terminal
    git push

    Now your branch contains everything on GitHub, so the push is a fast-forward.

How to verify it worked

  • git push finishes with main -> main, and git status says 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.

Practice these as flashcards →

Terms in this scenario

Related scenarios

My notes

Saved in this browser only.