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.
- Publish a Repo to a Remoteeasy 4 min
- Rebase a Feature Branch onto mainmedium 6 min
- A Branch Tracking the Wrong Remote Brancheasy 6 min
No account needed for your first tries.
Related errors
- CONFLICT (content): Merge conflict in ... Automatic merge failed
Both branches changed the same lines, and Git needs you to choose.