Git push to GitLab rejected (fetch first): causes and fixes

By Melih Toksari, developer of MergeBell · Updated · 4 min read

Short answer

Git rejects a push with "! [rejected] main -> main (fetch first)" when the branch on GitLab has commits your local branch doesn't: often the README GitLab created with a new project, a teammate's push, or a merged merge request. "non-fast-forward" means the same once you've fetched those commits, or that you rewrote commits you had already pushed. Run git pull --rebase origin main, fix any conflicts, and push again; it also works for a new project's README, whose history is unrelated to yours. To replace what's on GitLab instead, use git push --force-with-lease, but GitLab blocks force pushes to protected branches, including the default branch, unless a Maintainer allows them.

You push to GitLab, and Git refuses:

To https://gitlab.com/mygroup/my-project.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://gitlab.com/mygroup/my-project.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.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

GitLab’s main has commits your local main doesn’t, and pushing would throw them away, so Git stops before sending anything. Here’s where those commits come from, and how to push without losing them, with GitLab 19.5.

Fetch first or non-fast-forward

  • (fetch first): GitLab’s branch has commits you’ve never downloaded.
  • (non-fast-forward): you’ve downloaded them, with git fetch for example, but your branch doesn’t include them. When it’s the branch you have checked out, the hint starts with “Updates were rejected because the tip of your current branch is behind its remote counterpart.”
  • After git commit --amend or a rebase of commits you had already pushed, your branch no longer includes the originals, and the push is rejected as non-fast-forward too.

The commits on GitLab usually come from:

  • A new project with a README. Initialize repository with a README makes a first commit on GitLab when you create the project. Your local repository has its own history, with no commit in common.
  • Someone else’s push, or a merge request merged into the branch.
  • An edit in GitLab itself, in the web editor or the Web IDE.
  • A job or bot that pushes, such as a release job that bumps a version number.

Fix it: rebase onto GitLab’s branch

git pull --rebase origin main
git push origin main

git pull --rebase downloads GitLab’s commits and replays yours on top of them, so the push only adds commits. If one of your commits conflicts, Git stops: fix the files, git add them, and run git rebase --continue, or git rebase --abort to go back.

A new project with a README

The same commands work for a new project’s README, even though the two histories are unrelated: the rebase puts your commits on top of GitLab’s first commit. If your repository has its own README.md, the rebase stops with a conflict in it. Keep the version you want, then run git add README.md and git rebase --continue.

Check the branch name too. If your local branch is master and GitLab’s is main, git push -u origin master doesn’t fail: it creates a second branch, master, and the project still shows only the README on main. Rename yours first:

git branch -M main
git pull --rebase origin main
git push -u origin main

Merge instead

git pull without --rebase merges, and recent versions of Git make you choose first:

hint: You have divergent branches and need to specify how to reconcile them.
…
fatal: Need to specify how to reconcile divergent branches.

Run git pull --no-rebase origin main to merge. For a new project’s README, Git also refuses with fatal: refusing to merge unrelated histories, so add --allow-unrelated-histories. To make rebasing the default, run git config --global pull.rebase true.

Replace what’s on GitLab

If GitLab’s commits are disposable, such as the README of a project you just created, or the old versions of your own commits after an amend or rebase, you can overwrite them. Fetch, check what you’d lose, and push with --force-with-lease, which refuses if the branch changed since you fetched:

git fetch origin
git log --oneline main..origin/main
git push --force-with-lease origin main

On GitLab, the default branch is protected by default, and a protected branch doesn’t accept force pushes unless a Maintainer turns on Allowed to force push for it:

remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To https://gitlab.com/mygroup/my-project.git
 ! [remote rejected] main -> main (pre-receive hook declined)

That’s GitLab’s rule, not Git’s: see GitLab protected branch errors. On a branch others push to, rebase instead, because a force push deletes their commits from it.

The other push rejections

Git says What it means
! [rejected] … (fetch first) GitLab’s branch has commits you haven’t fetched
! [rejected] … (non-fast-forward) Your branch is behind GitLab’s, or you rewrote commits you had pushed
! [rejected] … (stale info) --force-with-lease refused, because the branch changed after your last fetch
! [rejected] … (already exists) GitLab already has a tag with that name
! [remote rejected] … (pre-receive hook declined) Git sent the push, and GitLab refused it: see GitLab “pre-receive hook declined”

[rejected] comes from Git on your computer, and [remote rejected] from GitLab. If Git can’t sign in at all, the error is different: see HTTP Basic: Access denied, or Permission denied (publickey) over SSH.

On your phone

A rejected push happens on your computer, so the fix starts there. Once your branch is pushed, MergeBell shows its merge request, pipeline and approvals on your phone, and you can approve and merge from there on the free tier. See how to review a GitLab merge request on your phone.

Frequently asked questions

Why does Git say "Updates were rejected because the remote contains work that you do not have locally" when I push to GitLab?

The branch on GitLab has commits that aren't in your local repository, and pushing would throw them away. The usual causes are the README GitLab adds to a new project, a teammate's push, a merged merge request, or an edit in GitLab's web editor. Run git pull --rebase origin main, resolve any conflicts, and push again.

How do I push an existing repository to a new GitLab project that already has a README?

If your local branch is master, rename it first with git branch -M main. Then add the project as a remote, run git pull --rebase origin main to put your commits on top of GitLab's README commit, and git push -u origin main. If your repository has its own README.md, the rebase stops with a conflict: keep the version you want, run git add README.md, then git rebase --continue.

How do I fix "fatal: refusing to merge unrelated histories"?

git pull tried to merge two histories with no commit in common, usually your local repository and a new GitLab project's first commit. Rebase with git pull --rebase origin main, or merge on purpose with git pull --no-rebase --allow-unrelated-histories origin main.

Can I force push to GitLab instead?

Only if you mean to replace what's on GitLab. Run git fetch, check what you'd lose, then git push --force-with-lease, which refuses if the branch changed since you fetched. GitLab blocks force pushes to protected branches, and the default branch is protected by default, with "You are not allowed to force push code to a protected branch on this project." A Maintainer can turn on Allowed to force push for that branch.

What does "fatal: Need to specify how to reconcile divergent branches" mean?

Your branch and GitLab's each have commits the other doesn't, and Git doesn't know whether git pull should rebase or merge. Run git pull --rebase or git pull --no-rebase, or set a default with git config --global pull.rebase true.

Sources

Checked against these pages on 18 November 2026. GitLab changes quickly — if something here no longer matches, tell us and we will fix it.

Do this from your phone with MergeBell

Free for the whole review loop: the To-Do inbox, diffs and line comments, approve and merge, pipelines with retry, and push alerts for your To-Dos. Works with gitlab.com and self-managed GitLab 16.0+.

Get MergeBell for iPhoneGet MergeBell for Android

On the App Store for iOS 26 or later and on Google Play for Android 12 or later. An independent app, not affiliated with GitLab Inc.