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.
main has diverged:
Git only allows a push when your branch is a fast-forward of the remote tip. Here it is not.
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.
git add <fixed-files>git rebase --continuegit push origin mainTo abort a messy rebase and return to the pre-rebase state:
git rebase --abort
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.
git fetch origin
git status
git log --oneline --left-right HEAD...origin/main
<) are yours not yet on the remote.>) are on the remote and not in your HEAD — pull/rebase first.git pull --rebase origin main (or fetch + rebase) before starting a new local commit batch when the same repo is updated from GitHub Actions, another PC, or the web editor.main, or use feature branches and open a PR so direct races on main are rarer.Tech note for the portfolio Notes → Tech stream. Based on a real hhhimel.github.io push rejection.