git pull --rebase (or git pull), resolve any conflicts, then push again. If the branch is protected, push to a new branch and open a pull request. Use git push --force-with-lease only for your own branch after a rebase.
The messages and what they mean
| Message | Meaning | Fix |
|---|---|---|
| rejected (fetch first) / non-fast-forward | Remote has new commits | git pull --rebase, then push |
| protected branch hook declined | Direct pushes to main are blocked | Push a branch, open a PR |
| src refspec main does not match any | No local commit or branch named main | Commit first; check git branch (maybe it is master) |
| Permission denied (publickey) / 403 | Authentication problem | Fix SSH key or use a token; check repo access |
| remote: error: File is 120 MB; exceeds 100 MB | File too large for GitHub | Remove it from history; use Git LFS |
Fix the usual case
- Fetch and replay your commits on top of the remote:
git pull --rebase origin main. - If there are conflicts, fix them,
git addthe files and rungit rebase --continue. - Run the tests.
- Push:
git push origin main(or your branch).
git pull --rebase origin main
# fix conflicts if any, then
git push origin main When force push is OK, and when it is not
After rebasing your own feature branch, the remote history differs from yours, so a normal push is rejected. Use git push --force-with-lease: it overwrites only if nobody else pushed since you last fetched.
Never force push to main or a shared branch. It can delete teammates' commits. Most teams block it with branch protection rules.
Prevent it
- Start each task from an updated main:
git switch main && git pull. - Work on feature branches and merge through pull requests.
- Set
git config --global pull.rebase trueif your team prefers a linear history.
Questions
Will git pull delete my work?
No. It merges or rebases the remote commits with yours. If something goes wrong, git reflog shows every previous state so you can go back.
What is the difference between --force and --force-with-lease?
--force overwrites the remote no matter what. --force-with-lease refuses if the remote has commits you have not seen, which protects teammates' work.