Your repository on GitHub · lesson 7 of 9
Commands and ideas in this lesson
A! [rejected]the remote has commits you don't
Bgit pull, git pushthe fix
Cgit push --forceoverwrites the remote: not the fix
Dpull.rebase, pull.ffchoose what pull does

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:

Terminal
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

Git sandbox
  1. Try git push, and read why it’s rejected
  2. Pull Joaquin’s commit in with a merge: git pull --no-rebase
  3. 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

  1. 4b9596c HEAD →main Add halo-halo recipe
  2. 67e435d origin/main Cover the leche flan with foil
  3. ba04ca2 Add lumpiang shanghai recipe
  4. 98a5edc Add a script that builds the index page
  5. d078487 Make the sinigang steps clearer
  6. 145dc1e Add pancit bihon recipe
  7. 52d1f4d Say how much salt goes in the adobo
  8. 1c17fad Add a README
  9. 9c53888 Add leche flan recipe
  10. 1f359a2 Add sinigang recipe
  11. 601c654 Add 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
  1. 4b9596c HEAD →main Add halo-halo recipe
  2. 18248d8 origin/main Add more calamansi to the pancit
  3. 67e435d Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe

and 2 older commits

1 of 3 The hosted main has Joaquin’s calamansi commit. Ana’s main has her halo-halo commit instead.

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.

Git sandbox
  1. Set this repository to rebase when pulling
  2. 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"

Spot the trapAna

Ana’s push is rejected. An answer online says: “Use git push --force, it always works.”

What happens if…

Check what you learned

Why was Ana’s push rejected?
What’s the right fix for a rejected push on a shared branch?
What does git config --global pull.rebase false do?

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.

All sources for Simple Git