/errors/git/failed-to-push-some-refs
error: failed to push some refs (rejected, fetch first)
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · git version 2.55.0.windows.5updated 2026-10-20
- tool
- Git
- exit code
- 1
- versions
- Git 2.x
- os
- linux, macos, windows
git push fails with ! [rejected] main -> main (fetch first) and error: failed to push some refs when the remote branch contains commits that your local branch does not have. Git refuses to overwrite them. Fetch those commits and integrate them, then push again.
Reproduce the error
Two clones of the same repository; the first one pushes, then the second one tries to:
git init -q --bare remote.git
git clone -q remote.git alice 2>/dev/null && cd alice
git switch -q -c main && echo one > a.txt && git add a.txt && git commit -qm "one" && git push -q origin main
cd .. && git clone -q -b main remote.git bob && cd alice
echo two >> a.txt && git commit -qam "two" && git push -q origin main
cd ../bob
echo three > b.txt && git add b.txt && git commit -qm "three"
git push origin mainOutput · exit 1
To /tmp/work/remote.git ! [rejected] main -> main (fetch first) error: failed to push some refs to '/tmp/work/remote.git' hint: Updates were rejected because the remote contains work that you do not hint: have locally. This is usually caused by another repository pushing to hint: the same ref. If you want to integrate the remote changes, use hint: 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Fix: rebase onto the remote, then push
git init -q --bare remote.git
git clone -q remote.git alice 2>/dev/null && cd alice
git switch -q -c main && echo one > a.txt && git add a.txt && git commit -qm "one" && git push -q origin main
cd .. && git clone -q -b main remote.git bob && cd alice
echo two >> a.txt && git commit -qam "two" && git push -q origin main
cd ../bob
echo three > b.txt && git add b.txt && git commit -qm "three"
git pull --rebase -q origin main
git push -q origin main && git log --onelineOutput
2b24f0e three e76c590 two e057f16 one
git pull --rebase replays your local commits on top of the remote ones, so the push becomes a fast-forward. A plain git pull (merge) works too, but adds a merge commit.
Sources
Related pages
flags · gitgit push --force-with-lease: a safer force push--force-with-lease overwrites the remote branch only if it still points where your remote-tracking ref says, so commits pushed since your fetch are never lost.compare · gitgit merge vs git rebase: differences and when to use eachgit merge joins histories with a merge commit and keeps them as they happened; git rebase replays commits on a new base for a linear history with new hashes.errors · gitfatal: refusing to merge unrelated historiesGit refuses to merge two branches that share no common commit, for example a new local repo and a remote created with a README. Use --allow-unrelated-histories.