Fix guides · Cursor, Claude Code, Replit, Lovable ·

Fix "Updates were rejected" (fetch first) on git push

! [rejected] main -> main (fetch first) error: failed to push some refs ... Updates were rejected because the remote contains work that you do not have locally

This error means the branch on GitHub (the "remote") has commits that your local copy doesn't have, so Git refuses to push because it would overwrite them. This often happens when Lovable, Replit, a bot or an edit on github.com added commits to the same branch. The fix is to fetch and integrate those commits first, usually with `git pull --rebase origin main`, resolve any conflicts, then push again.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. An AI builder pushed to the same repo

How to tell: You use Lovable, Replit or a similar tool connected to the same GitHub repo. Running `git fetch origin` then `git log --oneline HEAD..origin/main` shows commits you didn't make on this machine.

Fix: Run `git pull --rebase origin main` to put your local commits on top of the tool's commits, then push. In the future, pull before you start editing locally.

2. You edited files on github.com

How to tell: You changed a README, merged a pull request, or edited a file directly in the GitHub web interface. Those commits show up in `git log HEAD..origin/main`.

Fix: Pull with rebase (`git pull --rebase origin main`), resolve any conflicts, and push.

3. A bot or CI workflow committed

How to tell: The extra commits are authored by something like Dependabot, a GitHub Actions workflow, or a release or formatting bot.

Fix: Pull the bot's commits in the same way before pushing. If a workflow keeps committing to main, expect to pull often or change the workflow to open pull requests instead.

4. Another computer or teammate pushed

How to tell: The commits on origin/main come from a different laptop, a Codespace, or another person.

Fix: Pull with rebase, fix any conflicts together, then push. Get into the habit of running `git pull` before starting work.

5. New GitHub repo created with a README or license

How to tell: You created the repo on GitHub with a README, .gitignore or license, then tried to push a project that already had its own history. Pulling then fails with "refusing to merge unrelated histories".

Fix: Run `git pull origin main --allow-unrelated-histories`, resolve conflicts (often just README or .gitignore), commit, then push.

How to fix it

  1. Save your local work: run `git status`, then either commit (`git add -A` and `git commit -m "wip"`) or stash it with `git stash`.
  2. Look at what's on GitHub: run `git fetch origin`, then `git log --oneline HEAD..origin/main` to list the commits you're missing.
  3. Bring those commits in: run `git pull --rebase origin main`. (Use `git pull --no-rebase origin main` if you prefer a merge commit.)
  4. If Git reports conflicts, open each listed file, keep the right code and delete the `<<<<<<<`, `=======` and `>>>>>>>` markers, then `git add <file>` and `git rebase --continue`. Use `git rebase --abort` to go back to how things were.
  5. If you stashed earlier, run `git stash pop`. Then run the app or tests to make sure everything still works.
  6. Push: `git push origin main`.
  7. Confirm with `git status` that your branch is up to date with 'origin/main'.

Using Lovable, Bolt or v0?

In Lovable, the GitHub connection syncs both ways on the default branch, so if you also push from your own computer or another tool, the two histories collide. Before editing outside Lovable, pull the latest changes, and avoid editing the same branch in two places at once. In Replit, use the Git panel to pull or sync before pushing. After the push works, check your GitHub repo and any connected host like Vercel or Netlify to confirm the newest commit appears and deployed.

Using Cursor, Claude Code or Windsurf?

Ask the agent to first run `git fetch origin` and show you `git log --oneline HEAD..origin/main` so you see exactly what's on GitHub before anything changes. Then have it run `git pull --rebase origin main`, explain each conflict before resolving it, and never use `--force`. To verify, ask it to run `git status` (it should say up to date with 'origin/main') and `git log origin/main..HEAD` (it should print nothing after the push).

The “fix” to avoid

Agents often reach for `git push --force` because it makes the error disappear instantly. It replaces GitHub's history with yours and permanently drops the remote commits, such as Lovable's edits or a teammate's work, and can break deployments. Equally bad is deleting the `.git` folder or re-cloning over your project, which can throw away your own uncommitted work.

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

My git push fails with: "! [rejected] main -> main (fetch first) error: failed to push some refs ... Updates were rejected because the remote contains work that you do not have locally". First run `git status` and make sure local changes are committed or stashed. Run `git fetch origin` and show me `git log --oneline HEAD..origin/main` so I can see which commits are on GitHub (e.g. from Lovable, a bot, or github.com edits). Then run `git pull --rebase origin main`. If you see "refusing to merge unrelated histories", use `git pull origin main --allow-unrelated-histories` instead. For any conflicts, explain each one and how you'll resolve it before continuing. Do not use `git push --force` or delete `.git`. When done, push to origin main and verify that `git status` says up to date and `git log origin/main..HEAD` is empty.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

What does "fetch first" mean in a git push error?

It means GitHub has commits your local copy doesn't know about yet. Git wants you to fetch and integrate them before it will accept your push.

Is it safe to use git push --force to fix this?

Usually not. Force-pushing deletes the remote commits you don't have, which can wipe out someone else's work or changes made by tools like Lovable. Only do it if you've checked those commits and are sure you want them gone.

Should I use git pull --rebase or a normal git pull?

Both work. `--rebase` keeps history in a straight line by replaying your commits on top of the remote ones, while a normal merge pull adds a merge commit. Pick one and stay consistent.

Official documentation

Related errors

Get fixes like this every week