← Back to all notes
8 Oct 2026 · 2 min read · Git

Git push rejected (fetch first): integrating remote commits safely

Problem

git push origin main fails with:

! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/…/….git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.

Your laptop has new commits. GitHub (or another machine) also has commits on main that are not in your local history. A plain push would overwrite that remote work, so Git refuses.

Cause

main has diverged:

Git only allows a push when your branch is a fast-forward of the remote tip. Here it is not.

Fix (safe — preferred)

Integrate remote work, then push. Prefer rebase if your local commits are only on this machine and you want a linear history:

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git add .
git rebase --continue
git push origin main

Or a merge pull (creates a merge commit):

git pull origin main
git push origin main

If git pull opens an editor for a merge message, save and close to finish.

If rebase reports conflicts

  1. Open the listed files and fix the conflict markers.
  2. git add <fixed-files>
  3. git rebase --continue
  4. When the rebase finishes: git push origin main

To abort a messy rebase and return to the pre-rebase state:

git rebase --abort

What not to do on a shared main

# Avoid on main if others use this repo:
git push --force origin main

Force-push rewrites remote history and can delete someone else’s commits. Use it only on a private branch you alone use, or after the team agrees.

Check before you push next time

git fetch origin
git status
git log --oneline --left-right HEAD...origin/main

What I would do differently


Tech note for the portfolio Notes → Tech stream. Based on a real hhhimel.github.io push rejection.

← Back to all notes