This error occurs when your local branch is behind the remote branch. Git prevents the push because someone else has pushed commits that you don't have locally. You need to fetch and integrate those changes before pushing.
When you see "rejected... fetch first" followed by "failed to push some refs," Git is telling you that your local repository is out of sync with the remote. The remote branch contains commits that your local branch doesn't have. Git uses a "fast-forward" model by default. This means your local commits must build directly on top of the latest remote commits. If someone else pushed changes while you were working, your commits no longer sit on top of the remote's latest state. This is actually a safety feature. Git refuses to push because doing so without fetching first could result in lost work - either yours or your teammates'. The error message is Git's way of saying "stop, sync up first, then try again."
First, download the new commits from the remote repository without modifying your local branch:
git fetch originThis updates your remote-tracking branches (like origin/main) so you can see what changed.
Before merging, check what commits exist on the remote that you don't have:
git log HEAD..origin/main --onelineThis shows commits on origin/main that aren't in your local branch. Understanding these changes helps you anticipate potential conflicts.
The simplest approach is to merge the remote changes into your local branch:
git pull origin mainOr if you've already fetched:
git merge origin/mainThis creates a merge commit combining both sets of changes. If there are conflicts, Git will pause and ask you to resolve them manually.
For a cleaner, linear history, you can rebase your commits on top of the remote changes:
git pull --rebase origin mainOr if you've already fetched:
git rebase origin/mainThis replays your local commits on top of the remote's latest state. The result is a straight-line history without merge commits.
If Git reports conflicts during merge or rebase, you'll need to resolve them:
1. Open the conflicting files and look for conflict markers (<<<<<<<, =======, >>>>>>>)
2. Edit the files to keep the correct content
3. Stage the resolved files: git add <file>
4. For merge: git commit to complete the merge
5. For rebase: git rebase --continue to proceed
If you get stuck during a rebase, you can abort with git rebase --abort.
Once you've integrated the remote changes, push again:
git push origin mainThe push should now succeed because your local branch includes all the remote commits plus your own.
Force Push (Use with Extreme Caution)
You can bypass this error with git push --force, but this is dangerous:
# DON'T do this unless you fully understand the consequences
git push --force origin mainForce pushing overwrites the remote branch with your local version, potentially deleting your teammates' commits permanently. Only use this if:
- You're the only person working on the branch
- You intentionally rebased and need to update the remote
- You're certain no one else's work will be lost
A safer alternative is --force-with-lease:
git push --force-with-lease origin mainThis only force pushes if no one else has pushed since your last fetch, providing a safety check.
Preventing This Error
- Pull before you push: Run git pull before starting work
- Push frequently: Smaller, more frequent pushes reduce conflicts
- Use feature branches: Work on separate branches and merge via pull requests
- Communicate with your team: Let others know when you're working on shared branches
Branch Name Differences
Note that GitHub and newer Git installations use main as the default branch, while older setups use master. Replace main with master in the commands if your repository uses that convention.
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