How to fix ! [rejected] main -> main (fetch first) error: failed to push some refs in Git
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- Fetch the latest changes from remote
- Review what changed on the remote
- Integrate remote changes (Option A: Merge)
- Integrate remote changes (Option B: Rebase)
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
- 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
- You created a new repository on GitHub with a README and then tried to push existing local commits
- Force push by another team member rewrote the branch history
How to fix "! [rejected] main -> main (fetch first) error: failed to push some refs"
First, download the new commits from the remote repository without modifying your local branch:
git fetch originThis updates your remote-tracking branches (like origin/main) so you can see what changed.
Before merging, check what commits exist on the remote that you don't have:
git log HEAD..origin/main --onelineThis shows commits on origin/main that aren't in your local branch. Understanding these changes helps you anticipate potential conflicts.
The simplest approach is to merge the remote changes into your local branch:
git pull origin mainOr if you've already fetched:
git merge origin/mainThis creates a merge commit combining both sets of changes. If there are conflicts, Git will pause and ask you to resolve them manually.
For a cleaner, linear history, you can rebase your commits on top of the remote changes:
git pull --rebase origin mainOr if you've already fetched:
git rebase origin/mainThis replays your local commits on top of the remote's latest state. The result is a straight-line history without merge commits.
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.
Once you've integrated the remote changes, push again:
git push origin mainThe 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:
# DON'T do this unless you fully understand the consequences
git push --force origin mainForce 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:
git push --force-with-lease origin mainThis 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
- ๐Git Official Documentation
- ๐docs.github.com - dealing with non fast forward errors(Official Docs)
- ๐ฌstackoverflow.com - rejected master master fetch first(Stack Overflow)
- ๐ฌstackoverflow.com - git error failed to push some refs to remote(Stack Overflow)
- ๐
- ๐happygitwithr.com - push rejected(Official Docs)