How to fix ! [rejected] main -> main (fetch first) error: failed to push some refs in Git

GitBEGINNERMEDIUMUpdated Dec 6, 2025ยท 391 views

This error occurs when your local branch is behind the remote branch. Git prevents the push because someone else has pushed commits that you don't have locally. You need to fetch and integrate those changes before pushing.

Quick fix

Fixes ! [rejected] main -> main (fetch first) error: failed to push some refs

git fetch origin
  1. Fetch the latest changes from remote
  2. Review what changed on the remote
  3. Integrate remote changes (Option A: Merge)
  4. Integrate remote changes (Option B: Rebase)
git โ€” error
git fetch origin
! [rejected] main -> main (fetch first) error: failed to push some refs

What this error means

When you see "rejected... fetch first" followed by "failed to push some refs," Git is telling you that your local repository is out of sync with the remote. The remote branch contains commits that your local branch doesn't have. Git uses a "fast-forward" model by default. This means your local commits must build directly on top of the latest remote commits. If someone else pushed changes while you were working, your commits no longer sit on top of the remote's latest state. This is actually a safety feature. Git refuses to push because doing so without fetching first could result in lost work - either yours or your teammates'. The error message is Git's way of saying "stop, sync up first, then try again."

Typical symptoms

  • Running 'git push' fails with '[rejected]' message
  • Error mentions 'fetch first' in parentheses
  • Message includes 'failed to push some refs'
  • Push worked previously but now fails
  • Happens after a teammate pushed to the same branch
  • CI/CD pipeline fails on push step

Common causes

  1. Another developer pushed commits to the same branch while you were working
  2. You worked on the branch from multiple machines without syncing
  3. A CI/CD system or bot pushed changes to the branch
  4. You created a new repository on GitHub with a README and then tried to push existing local commits
  5. Force push by another team member rewrote the branch history

How to fix "! [rejected] main -> main (fetch first) error: failed to push some refs"

1Fetch the latest changes from remote

First, download the new commits from the remote repository without modifying your local branch:

bash
git fetch origin

This updates your remote-tracking branches (like origin/main) so you can see what changed.

2Review what changed on the remote

Before merging, check what commits exist on the remote that you don't have:

bash
git log HEAD..origin/main --oneline

This shows commits on origin/main that aren't in your local branch. Understanding these changes helps you anticipate potential conflicts.

3Integrate remote changes (Option A: Merge)

The simplest approach is to merge the remote changes into your local branch:

bash
git pull origin main

Or if you've already fetched:

bash
git merge origin/main

This creates a merge commit combining both sets of changes. If there are conflicts, Git will pause and ask you to resolve them manually.

4Integrate remote changes (Option B: Rebase)

For a cleaner, linear history, you can rebase your commits on top of the remote changes:

bash
git pull --rebase origin main

Or if you've already fetched:

bash
git rebase origin/main

This replays your local commits on top of the remote's latest state. The result is a straight-line history without merge commits.

5Resolve any merge conflicts

If Git reports conflicts during merge or rebase, you'll need to resolve them:

1. Open the conflicting files and look for conflict markers (<<<<<<<, =======, >>>>>>>)
2. Edit the files to keep the correct content
3. Stage the resolved files: git add <file>
4. For merge: git commit to complete the merge
5. For rebase: git rebase --continue to proceed

If you get stuck during a rebase, you can abort with git rebase --abort.

6Push your changes

Once you've integrated the remote changes, push again:

bash
git push origin main

The push should now succeed because your local branch includes all the remote commits plus your own.

Advanced notes

Force Push (Use with Extreme Caution)

You can bypass this error with git push --force, but this is dangerous:

bash
# DON'T do this unless you fully understand the consequences
git push --force origin main

Force pushing overwrites the remote branch with your local version, potentially deleting your teammates' commits permanently. Only use this if:
- You're the only person working on the branch
- You intentionally rebased and need to update the remote
- You're certain no one else's work will be lost

A safer alternative is --force-with-lease:

bash
git push --force-with-lease origin main

This only force pushes if no one else has pushed since your last fetch, providing a safety check.

Preventing This Error

- Pull before you push: Run git pull before starting work
- Push frequently: Smaller, more frequent pushes reduce conflicts
- Use feature branches: Work on separate branches and merge via pull requests
- Communicate with your team: Let others know when you're working on shared branches

Branch Name Differences

Note that GitHub and newer Git installations use main as the default branch, while older setups use master. Replace main with master in the commands if your repository uses that convention.

Frequently asked questions

What does "! [rejected] main -> main (fetch first) error: failed to push some ref" mean?
When you see "rejected... fetch first" followed by "failed to push some refs," Git is telling you that your local repository is out of sync with the remote. The remote branch contains commits that your local branch doesn't have.
How do I fix "! [rejected] main -> main (fetch first) error: failed to push some ref"?
This error occurs when your local branch is behind the remote branch. Git prevents the push because someone else has pushed commits that you don't have locally. You need to fetch and integrate those changes before pushing.
What causes "! [rejected] main -> main (fetch first) error: failed to push some ref"?
Another developer pushed commits to the same branch while you were working You worked on the branch from multiple machines without syncing A CI/CD system or bot pushed changes to the branch
Is the "! [rejected] main -> main (fetch first) error: failed to push some ref" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading