When push is rejected
Last updated
Sooner or later, git push says "rejected". It feels like an error, but it's Git protecting a teammate's work. In this lesson you'll see why it happens, fix it the right way, see exactly what --force would have destroyed, and set up pull so it never surprises you.
By the end of this lesson you can
- Explain why a push is rejected when the remote has commits you don't
- Recover by pulling, settling any conflict, and pushing again
- Explain what git push --force would have done instead
- Choose how git pull integrates, with pull.rebase or pull.ff
Why Git rejects a push
Ana commits the halo-halo recipe. Meanwhile, Joaquin pushed a commit about calamansi. Ana's push:
git push To https://example.com/ana/kusina.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://example.com/ana/kusina.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.
A push moves the hosted main to Ana's commit. But Ana's commit doesn't come after Joaquin's: both came after the same commit. Moving main to Ana's would drop Joaquin's commit out of main's history. So Git only allows a push that's a fast-forward, and this isn't one.
Nothing is wrong with Ana's commit. The message means: "someone else got there first; take their work in, then try again".
Fix it: pull, then push
- Try
git push, and read why it’s rejected - Pull Joaquin’s commit in with a merge:
git pull --no-rebase - Push again
Working folder
no changes
Staging area
nothing staged
Repository
11 commits on main
Remote
origin: example.com/ana/kusina.git · you are 1 ahead (as of your last fetch)
You are in ~/kusina with one new commit. Joaquin pushed a commit to origin a few minutes ago.
Commit graph
4b9596cHEAD →main Add halo-halo recipe67e435dorigin/main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
If the pull had a conflict, you'd settle it as in lesson 3.4, commit, and then push. The rejected push waits patiently; nothing is lost.
What --force would have done
git push --force skips the fast-forward rule: the hosted branch is set to your commit, whatever was there. Here's that, replayed:
$ git fetch From https://example.com/ana/kusina.git 67e435d..18248d8 main -> origin/main $ git log --oneline --graph --all -3 * 4b9596c (HEAD -> main) Add halo-halo recipe | * 18248d8 (origin/main) Add more calamansi to the pancit |/ * 67e435d Cover the leche flan with foil
4b9596cHEAD →main Add halo-halo recipe18248d8origin/main Add more calamansi to the pancit67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe
and 2 older commits
git push --forceCan lose work: can throw away work Git never saved, or rewrite shared history on a shared branch throws away other people's pushed work. Lesson 6.3 shows the one place force is right (your own branch, after a rebase) and the safer --force-with-lease.
Choose what pull does
When both sides have new commits, git pull can combine them two ways:
- merge (
pull.rebase false): makes a merge commit, as you just did. - rebase (
pull.rebase true): lifts your new commits off and puts them after the ones you pulled, for a straight line. Lesson 6.2 explains rebase.
Or pull.ff only: only pull when it's a fast-forward, and stop otherwise, so you decide each time.
Set your choice once: git config --global pull.rebase false (or true). Until you do, Git stops and asks. Teams often agree on one; ask yours.
- Set this repository to rebase when pulling
- Pull, then look at the history: a straight line
Working folder
no changes
Staging area
nothing staged
Repository
11 commits on main
Remote
origin: example.com/ana/kusina.git · you are 1 ahead (as of your last fetch)
You are in ~/kusina with one new commit. Joaquin pushed a commit to origin a few minutes ago.
Spot the trap: "just use push --force"
Ana’s push is rejected. An answer online says: “Use git push --force, it always works.”
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-push, Note about fast-forwards: why non-fast-forward pushes are refused, and what --force loses
- Git documentation: git-pull: --rebase, --no-rebase, --ff-only
- Git documentation: git-config, pull.rebase and pull.ff: setting the default once