Git push rejected: fetch first and non-fast-forward errors
Fix "! [rejected] main -> main (fetch first)" and "failed to push some refs": pull the remote changes, handle protected branches and big files.
A rejected push means the remote branch has commits that your copy does not have, or the server refuses the push for another reason. The message after "rejected" tells you which. Do not reach for --force before you know why.
What to do, step by step
- Read the line in brackets: fetch first or non-fast-forward means the remote has new commits.
- Get them: git pull --rebase puts your commits on top of the remote ones. git pull makes a merge commit instead.
- If Git reports conflicts, fix the marked files, then git add them and git rebase --continue (or finish the merge).
- Push again: git push.
- Only if you rewrote history on purpose, such as after a rebase of your own branch, push with git push --force-with-lease. It refuses to overwrite work you have not seen.
Commands you can copy
git pull --rebasegit push --force-with-leasegit push origin HEAD:my-branchgit remote -vOther causes to check
- remote rejected: protected branch hook declined
- The branch needs a pull request or checks before changes. Push to a new branch with git push origin HEAD:my-branch and open a pull request.
- GH001: Large files detected
- GitHub blocks files over 100 MB. Remove the file from the commit, and use Git LFS for big files. Deleting it in a later commit is not enough, since it is still in history.
- Permission denied or 403
- Your account has no write access, or the saved login is for another account. Check with git remote -v and sign in with the right account or key.
- refusing to update checked out branch
- You pushed to a non-bare repository. Push to a bare repository, or use a different branch.
- Updates were rejected because the tag already exists
- A tag with that name is already on the remote. Use a new tag name, or delete the old one on purpose.
Questions about Git push rejected
- What does non-fast-forward mean?
- Your push would not simply add commits on top of the remote branch, because the remote has commits you lack. Git refuses so that nobody loses work.
- Is git push --force safe?
- It overwrites the remote branch with yours and can delete other people's commits. Use --force-with-lease, and only on a branch where you are sure.
- Should I pull with rebase or merge?
- Rebase keeps a straight history. Merge keeps the exact order things happened. Pick what your team uses.
Config generators for this
- SSH config for multiple GitHub accountsUse two or three GitHub accounts on one computer with separate SSH keys and host names.
- .gitconfig setupSet your name, email, default branch and shortcuts once.