0
0
GitDebug / FixBeginner · 4 min read

How to Fix Remote Rejected Push Error in Git

A remote rejected push in Git happens when the remote repository refuses your push, often due to conflicts or permission issues. To fix it, first pull and merge remote changes with git pull --rebase, resolve any conflicts, then push again with git push.
🔍

Why This Happens

This error occurs because the remote repository has changes that your local branch does not have. Git rejects your push to prevent overwriting those changes. It can also happen if you lack permission or if the remote branch is protected.

bash
git push origin main
Output
error: failed to push some refs to 'https://github.com/user/repo.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. fatal: Could not update refs/heads/main
🔧

The Fix

To fix this, first update your local branch with the remote changes using git pull --rebase. This applies your changes on top of the latest remote commits. If there are conflicts, resolve them, then continue the rebase. Finally, push your changes again.

bash
git pull --rebase origin main
git push origin main
Output
First, git pulls and rebases your changes: Successfully rebased and updated refs/heads/main. Then, git push uploads your commits: Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) To https://github.com/user/repo.git abc1234..def5678 main -> main
🛡️

Prevention

Always pull remote changes before pushing to keep your local branch updated. Use git pull --rebase to avoid unnecessary merge commits. Communicate with your team to avoid conflicting changes and ensure you have proper permissions to push.

⚠️

Related Errors

Other common errors include:

  • Permission denied: You lack rights to push. Fix by checking SSH keys or access rights.
  • Non-fast-forward: Happens when your push is behind remote. Fix by pulling first.
  • Protected branch: Remote branch is locked. Fix by pushing to a feature branch or getting permission.

Key Takeaways

Always pull remote changes with 'git pull --rebase' before pushing.
Resolve any merge conflicts before pushing again.
Check your permissions if push is rejected due to access issues.
Communicate with your team to avoid conflicting changes.
Use feature branches to avoid pushing directly to protected branches.