Founding prices until 15 December: 30% off Pro, locked for life. See pricing

$ … error: failed to push some refs to ... (fetch first / non-fast-forward)

Git 'failed to push some refs': rejected non-fast-forward

The remote branch has commits you don't have, so pushing would throw them away.

Git

What it means

Git only lets a push move a branch forward. If someone else pushed first, your branch and the remote's have diverged, and Git rejects the push so their commits aren't lost.

Causes and fixes

Most likely first. Run the check, and if it matches, apply the fix.

1. Someone pushed to the branch since you last pulled

Check:

git fetch
git log --oneline --graph HEAD origin/main -10

Fix: git pull --rebase (or merge), resolve any conflicts, then push.

2. You rewrote history (amend, rebase) on a branch already pushed

Check:

git status   # "have diverged"

Fix: On your own branch, git push --force-with-lease; never force-push a shared branch.

3. No upstream branch, or pushing to the wrong one

Check:

git branch -vv

Fix: git push -u origin <branch> the first time.

Fix it yourself

A live machine in your browser with this problem already on it. The grader checks the result.

Open the lab

No account needed for your first tries.

Related errors