You tried to push your code with git push and got a “rejected” error — now what?
This usually means the remote repository (the version of your project stored online, like on GitHub) has changes that you don’t have locally. Git won’t let you push because it could overwrite those changes.
Don’t worry, this is a normal part of working with Git, especially when collaborating. This guide shows you how to resolve it safely.
目次
Why git push gets rejected
The “rejected” error happens for these main reasons:
- Someone else pushed changes you don’t have yet: The remote branch is ahead of your local branch, so Git blocks your push to prevent data loss
- You worked on different devices without syncing: You pushed from one computer, then tried to push different changes from another
- Branch protection rules: The repository may have rules that prevent direct pushes to certain branches like
main
Solution 1: Pull first, then push (most common fix)
The standard fix is to download the remote changes first, merge them with yours, and then push.
1. Pull the latest changes from the remote
# Download and merge remote changes into your local branch
git pull origin mainReplace main with your branch name if it’s different (e.g., master or develop).
2. If there are no conflicts, push again
# Now your local branch includes the remote changes — push again
git push origin mainIf the push succeeds, you’re done.
3. If there are merge conflicts, resolve them
# Git will tell you which files have conflicts
# Open the conflicting files — look for these markers:
<<<<<<< HEAD
your changes here
=======
remote changes here
>>>>>>> origin/mainEdit the file to keep the code you want, removing the conflict markers. Then:
# Stage the resolved files
git add .
# Complete the merge
git commit -m "Resolve merge conflicts"
# Push again
git push origin mainIf the push goes through, you’re all set.
Solution 2: Use git pull –rebase for a cleaner history
If you prefer a linear commit history (without merge commits), use rebase instead.
1. Pull with rebase
# Replay your local commits on top of the remote changes
git pull --rebase origin mainThis takes your local changes, temporarily removes them, pulls the remote changes, then puts your changes back on top.
2. Push after the rebase completes
git push origin mainIf the push works, you’re done. The commit history will be clean and linear.
3. If conflicts occur during rebase
# Fix the conflicts in the affected files, then:
git add .
git rebase --continue
# If you want to abort and go back to how things were:
git rebase --abortAfter resolving conflicts and continuing the rebase, push again.
Solution 3: Push to a new branch instead
If you’re not sure how to handle the conflicts, the safest approach is to push your work to a new branch.
1. Create a new branch from your current work
# Create and switch to a new branch
git checkout -b my-feature-branch2. Push the new branch
# Push to the new branch — this won't be rejected
git push origin my-feature-branchSince this is a brand-new branch, there’s nothing to conflict with.
3. Create a pull request to merge later
On GitHub, you can then create a pull request (PR) to merge your branch into the main branch. This lets you review the changes before merging.
If nothing works
If the push is still being rejected:
- Check branch protection rules: On GitHub, go to Settings → Branches to see if the branch has protection rules that block direct pushes. You may need to use a pull request instead
- Check if you have write access: Make sure you have permission to push to the repository. If it’s someone else’s repo, you’ll need to fork it and submit a pull request
- Avoid force push unless you know what you’re doing:
git push --forcewill overwrite the remote — this can destroy other people’s work. Only use it on your own personal branches when you’re sure no one else is working on them
# Check which remote you're pushing to
git remote -v
# Check the current status of your branch
git status
git log --oneline -5- Tip for asking for help: Include the full error message, the output of
git status, and the output ofgit log --oneline -5to help others understand your situation
Summary
- A rejected push usually means the remote has changes you don’t have — run
git pullfirst, then push again - Use
git pull --rebaseif you want a cleaner commit history without merge commits - When in doubt, push to a new branch and create a pull request — this is the safest option
Related articles:
- pip-install-error.html (How to fix pip install errors)
- syntax-error-invalid-syntax.html (SyntaxError: invalid syntax — causes and fixes)

















Leave a Reply