Git refused your push because GitHub has commits you don't have yet. Nothing is broken and nothing is lost. Get their work first, then push again.
The error
You run git push and see something like this:
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:you/shop-app.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 already fetched their commits but have not merged them, Git says (non-fast-forward) and “the tip of your current branch is behind its remote counterpart” instead. The cause and the fix are the same.
What it means
Someone pushed to the same branch after you last pulled. It might be a teammate, or you on another computer, or an edit you made in the GitHub website. GitHub will not let your push replace commits you have never seen, because that would delete their work. This is Git protecting the team, not a bug.
Check first (these commands change nothing)
git status # which branch am I on? anything half done?
git fetch # download new commits, leave my files alone
git log --oneline main..origin/main # commits on GitHub that I don't have
git log --oneline origin/main..main # my commits that GitHub doesn't have
main..origin/main reads as “commits in origin/main that main does not have yet”. Now you know exactly what you are about to combine.
The fix
Bring their commits into your branch, then push:
git pull # fetch, then merge their work into yours
git push # now it goes through
If you prefer a straight line of history with no merge commit, use git pull --rebase instead. It puts your commits on top of theirs. Many teams set this as the default with git config --global pull.rebase true.
If the pull stops with a conflict
You and the other person changed the same lines. Git marks them in the file with <<<<<<<, ======= and >>>>>>>.
- Run
git status. It lists the files with conflicts. - Open each file, keep the right lines, and delete the three marker lines.
- Run
git addon each fixed file. - Finish with
git commit(after a merge) orgit rebase --continue(after a rebase). - Run
git push.
Want to back out instead? git merge --abort or git rebase --abort puts everything back the way it was before the pull.
Do not force push. git push --force makes the error go away by deleting the other person's commits from GitHub. On a shared branch that loses a teammate's work. Many teams block it with a branch rule.
Make it happen less often
- Pull before you start work each day, and before you push.
- Work on your own branch, not on
main. Then a push only touches your branch, and your work joinsmainthrough a pull request. - Push small and often. The fewer commits you hold back, the smaller any conflict is.
Other push errors that look similar
| You see | It means |
|---|---|
Permission denied (publickey) |
GitHub doesn't have your SSH key. Check with ssh -T git@github.com, or use the HTTPS address. |
Permission to OWNER/REPO.git denied to USER or 403
|
You are signed in as the wrong account, or that account can't write to the repo. Check with gh auth status. |
remote: Repository not found. |
A typo in the URL, or a private repo your account can't see. Check with git remote -v. |
GH013: Repository rule violations |
A rule blocks direct pushes to that branch, or a secret was found. Push a branch and open a pull request. |
New to the undo side of Git? Read git reset vs revert vs restore.
This guide first appeared on jeysingh.com. It comes from my Git / GitHub Study Guide. There's a free sample at jeysingh.com/git.
Top comments (0)