Your Code. Your Rules.

Fetch First / Non-Fast-Forward

! [rejected] main -> main (fetch first) - (non-fast-forward) - Updates were rejected because the remote contains work that you do not have locally.

Push Rejected: Fetch First / Non-Fast-Forward

Symptom

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://<INSTANCE>.git.eu/<USERNAME>/<REPO>.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.

After a git fetch, the same push reports:

 ! [rejected]        main -> main (non-fast-forward)
hint: Updates were rejected because the tip of your current branch is behind

Cause

Someone else pushed to the branch since your last pull. Your push would drop their commits, so git refuses it. fetch first means your clone does not know their commits yet; non-fast-forward means it knows them but your branch does not contain them.

Diagnose

Fetch, then list the commits on each side:

git fetch origin
git log --oneline main..origin/main
git log --oneline origin/main..main

The first list holds the commits you do not have, the second the ones the server does not have.

Fix

Option A - Rebase on the remote branch (recommended)

git pull --rebase origin main
git push

The rebase ends with Successfully rebased and updated refs/heads/main., then the push lands. On a conflict, fix the files, git add them and run git rebase --continue.

Option B - Merge the remote branch

git pull --no-rebase origin main
git push

This keeps both lines of work and adds a merge commit.

Option C - Overwrite the remote branch (⚠ drops other people's commits)

git push --force-with-lease origin main

Verify

git status

It prints Your branch is up to date with 'origin/main'.

Prevent

  • Run git pull --rebase before you start work and before you push.
  • Work on your own branch and merge it when it is ready.

Related