When a Push Is Rejected
Fetch first.
You commit, you push, and Git says rejected. Don't panic. It almost always means someone else pushed first, and GitHub has commits you don't have yet.

- Push rejected!
- Someone pushed first
- GitHub has commits you don't
Rejected
Alex pushed a commit while we were working. We made our own commit, and now we push. Git rejects it. The hint explains why. The remote contains work that you do not have locally. Git won't let us overwrite Alex's work.
git log --oneline -n 1 1333449 (HEAD -> main) Style the footer git push To https://github.com/sam/my-site.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/sam/my-site.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. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
The fix: pull, then push
The fix is simple. First, git pull, to bring in Alex's commit. On a new computer, Git may stop and ask how to combine, or reconcile, the two lines of work. Tell it once, with git config dash dash global, pull dot rebase false. That means merge, which is the classic behaviour. Now pull again. Our branches had both moved, so pull makes a merge commit.
git pull From https://github.com/sam/my-site f63a17a..7893b97 main -> origin/main hint: You have divergent branches and need to specify how to reconcile them. hint: You can do so by running one of the following commands sometime before hint: your next pull: hint: hint: git config pull.rebase false # merge hint: git config pull.rebase true # rebase hint: git config pull.ff only # fast-forward only hint: hint: You can replace "git config" with "git config --global" to set a default hint: preference for all repositories. You can also pass --rebase, --no-rebase, hint: or --ff-only on the command line to override the configured default per hint: invocation. fatal: Need to specify how to reconcile divergent branches. git config --global pull.rebase false git pull --no-edit Merge made by the 'ort' strategy. index.html | 1 + 1 file changed, 1 insertion(+)
Then push
Now git push works. The graph shows both commits, joined by the merge. Alex's commit and ours, side by side.
git push To https://github.com/sam/my-site.git 7893b97..ee3032f main -> main git log --oneline --graph -n 4 * ee3032f (HEAD -> main, origin/main) Merge branch 'main' of https://github.com/sam/my-site |\ | * 7893b97 Mention the blog * | 1333449 Style the footer |/ * f63a17a Link to about page
Never force it
You may see advice online to use git push dash dash force. That overwrites GitHub with your version, and throws away your teammates' commits. On a shared branch, never do it. Pull, then push. That's the answer, almost every time.
Summary
A rejected push means GitHub has new commits. Pull, then push. Never force push a shared branch. Next, pushing branches other than main.
Key takeaways- Rejected = pull first
- git pull, then git push
- Never force a shared branch