Skip to content
FR
  1. LuzineDebug
  2. Git & GitHub
  3. ! [rejected] main -> main (fetch first)
Git & GitHub Beginner ≈ 5 min to fix

Fix "failed to push some refs" (! [rejected] fetch first) in Git

Error message
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:user/repo.git'
  • ! [rejected] main -> main (non-fast-forward)
  • hint: Updates were rejected because the remote contains work that you do not have locally.
  • hint: Updates were rejected because the tip of your current branch is behind its remote counterpart.
  • error: src refspec main does not match any
  • ! [remote rejected] main -> main (protected branch hook declined)
In short

The remote repository has commits you do not have locally, so Git refuses to overwrite its history. Fetch them with git pull --rebase origin main, resolve any conflicts, then run git push again.

Be the first to confirm this fix

What this error means

By default, a git push is only accepted if it is a fast-forward: the remote branch must be an ancestor of your local branch, so no remote commit is lost. If someone (or you from another machine or the GitHub web UI) pushed in the meantime, or if you rewrote your history with rebase or commit --amend, the two histories diverge and the server rejects the update. The reason in parentheses matters: fetch first and non-fast-forward mean "integrate the remote first", remote rejected comes from a server rule, and src refspec ... does not match any means the local branch does not exist.

Common causes

  1. New commits on the remote (fetch first): a teammate pushed, or you edited a file (README…) directly on GitHub.
  2. Rewritten local history (non-fast-forward): after git rebase, git commit --amend or git reset, your branch no longer contains the commits already pushed.
  3. Branch does not exist (src refspec main does not match any): no commit yet, or your branch is called master and you push main.
  4. Protected branch (remote rejected, protected branch hook declined): the server forbids direct pushes and requires a pull request or checks.
  5. Remote repository created with a README while your local repository was initialised separately: the histories have no common ancestor.

Step-by-step fix

  1. Read the rejection reason

    Look at the text in parentheses after ! [rejected] or ! [remote rejected], then compare your history with the remote.

    bash
    git fetch origin
    git status
    git log --oneline --graph --all -n 20
  2. Integrate the remote commits

    Replay your local commits on top of the remote ones. Rebase keeps a linear history; if you prefer a merge commit, use git pull --no-rebase origin main.

    bash
    git pull --rebase origin main
  3. Resolve conflicts

    If Git stops on a conflict, open the listed files, keep the right content between the <<<<<<< and >>>>>>> markers, then continue. git rebase --abort cancels everything and brings you back to where you were.

    bash
    git status
    # edit the conflicted files, then:
    git add path/to/file
    git rebase --continue
  4. Push again

    Your branch now contains the remote history: the push is a fast-forward and goes through.

    bash
    git push origin main
  5. If you rewrote history on purpose

    Only on your own working branch, after an intentional rebase: use --force-with-lease, which refuses to overwrite the remote if someone pushed since your last fetch. Never use --force on main or a shared branch: you would erase other people’s work.

    bash
    git push --force-with-lease origin my-feature
  6. "src refspec does not match any" or protected branch

    Check your branch name and that at least one commit exists, then push the current branch. For a protected branch, push a feature branch and open a pull request.

    bash
    git branch --show-current
    git log --oneline -n 1
    git push -u origin HEAD

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Run git pull --rebase before you start working and before each push.
  • Set the default behaviour with git config --global pull.rebase true to avoid needless merge commits.
  • Work on feature branches and protect main: only pull requests get in.

Frequently asked questions

Can I just run git push --force?

Only if you are sure the remote commits must disappear, for example on your personal branch. Prefer --force-with-lease, and never on a shared branch.

What is the difference between fetch first and non-fast-forward?

fetch first: the remote has commits you never fetched. non-fast-forward: you already fetched them but your branch diverged. In both cases, integrate the remote with git pull --rebase.

Why do I get "hint: You have divergent branches" when I run git pull?

Since Git 2.27, Git asks how to reconcile two divergent histories. Pick a strategy: git pull --rebase once, or git config --global pull.rebase true for good.

Useful tools for this error

More Git & GitHub errors

All Git & GitHub errors →