This error occurs when Git cannot push your local commits to the remote repository because your local branch is behind the remote. The fix usually involves pulling remote changes first, then pushing again.
When you see "failed to push some refs to 'origin'", Git is telling you that the remote branch has commits that your local branch doesn't have. Git refuses to push because doing so would overwrite those commits on the remote. This is a non-fast-forward error. In Git terminology, a "fast-forward" is when your local branch can simply move forward to include all the remote commits. When that's not possible (because both branches have diverged with different commits), Git blocks the push to protect the remote commits. This commonly happens when: - A teammate pushed to the same branch before you - You pushed from a different machine and forgot to pull on your current machine - GitHub's web interface was used to edit files directly - CI/CD pipelines or bots made commits to the branch
The most common fix is to fetch and merge remote changes before pushing:
git pull origin <branch-name>For example, if you're on the main branch:
git pull origin mainThis fetches the remote changes and merges them with your local commits. If there are no conflicts, you can then push:
git push origin mainIf you prefer a linear commit history without merge commits, use rebase instead:
git pull --rebase origin <branch-name>This replays your local commits on top of the remote commits. After rebasing:
git push origin <branch-name>When to use rebase:
- Working on a feature branch
- You want a clean, linear history
- Your local commits haven't been shared with others yet
Ensure your local branch name matches the remote. GitHub changed the default branch from master to main:
# Check your current branch
git branch
# Check remote branches
git branch -r
# If you need to push to 'main' instead of 'master':
git branch -M main
git push -u origin mainIf you're on master locally but the remote only has main:
git checkout master
git branch -M main
git push origin mainWarning: Force pushing overwrites remote history. Only use this when you understand the consequences.
If you intentionally rewrote history (via rebase or amend) and need to update the remote:
# Safer option - fails if someone else pushed
git push --force-with-lease origin <branch-name>
# Destructive option - use with extreme caution
git push --force origin <branch-name>Never force push to shared branches like main or develop unless you've coordinated with your team. This can cause others to lose their work.
If git pull results in conflicts, you'll need to resolve them:
# See which files have conflicts
git status
# Open conflicting files and resolve manually
# Look for markers: <<<<<<<, =======, >>>>>>>
# After resolving, stage the files
git add <resolved-file>
# Complete the merge
git commit
# Now push
git push origin <branch-name>To abort the merge and return to the previous state:
git merge --abortIf pull and push still fail, the remote may have restrictions:
GitHub/GitLab branch protection:
- Go to repository Settings > Branches
- Check if the branch requires pull requests
- You may need to create a PR instead of pushing directly
Pre-receive hooks:
- Server-side hooks can reject pushes for various reasons
- Check with your repository administrator
Azure DevOps policies:
# If policies require PRs, create a branch and push there
git checkout -b feature/my-changes
git push -u origin feature/my-changes
# Then create a PR through the web interface### Understanding Fast-Forward vs Non-Fast-Forward
A fast-forward push is only possible when your local branch contains all commits from the remote plus your new commits. When both branches have unique commits, Git requires a merge or rebase.
Remote: A -- B -- C
Local: A -- B -- D
Cannot fast-forward because C and D diverge from BAfter git pull:
Local: A -- B -- C -- D (merge commit)
\-- D --/Or after git pull --rebase:
Local: A -- B -- C -- D' (D replayed on top of C)### When Force Push is Appropriate
Force push is legitimate when:
- You're working alone on a feature branch
- You ran git commit --amend to fix a commit message
- You rebased to incorporate upstream changes
- You're cleaning up a PR branch before final merge
Always use --force-with-lease instead of --force. It fails safely if someone else pushed to the branch:
git push --force-with-lease origin feature-branch### Large File Issues
If the error mentions file size limits:
# Check for large files
git rev-list --objects --all | \
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
awk '/^blob/ {print $3, $4}' | sort -rn | head -20
# Remove large files from history with git-filter-repo
pip install git-filter-repo
git filter-repo --path <large-file> --invert-paths
# Or use Git LFS for legitimate large files
git lfs install
git lfs track "*.psd"
git add .gitattributes### CI/CD Pipeline Considerations
If this error occurs in CI/CD:
- Ensure the pipeline has the latest code before pushing
- Use git fetch --all followed by proper merge/rebase
- Consider using --force-with-lease for deployment branches
- Set up proper credentials with write access
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git