This error occurs when the remote branch has commits that your local branch doesn't have. Git refuses the push to prevent overwriting those commits. The solution is to fetch and integrate the remote changes before pushing.
When you try to push commits to a remote repository, Git checks whether your push is a "fast-forward" update. A fast-forward means your new commits can simply be appended to the remote branch without losing any history. A non-fast-forward push means the remote branch has diverged from your local branchโsomeone else has pushed commits that you don't have locally. If Git allowed your push, those commits would be lost forever. This is why Git rejects the push with the "non-fast-forward" error. This commonly happens when: - A teammate pushed changes to the same branch while you were working - You rebased or amended commits that were already pushed - You're working on a shared branch without pulling regularly - CI/CD systems or bots have made commits to the branch
First, download the remote changes without modifying your working directory:
git fetch originThis updates your remote-tracking branches (like origin/main) so you can see what's different.
Before merging, understand 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. You can also see a visual comparison:
git log --oneline --graph --allThe 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 that combines both histories. If there are conflicts, Git will pause and let you resolve them.
For a cleaner, linear history, 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 branch. If there are conflicts, resolve them and run:
git rebase --continueAfter integrating the remote changes, push again:
git push origin mainThe push should succeed because your local branch now includes all commits from the remote.
If you used git commit --amend or git rebase to intentionally modify commits that were already pushed, you may need to force push:
git push --force-with-lease origin mainWARNING: This overwrites the remote branch. Only do this if:
- You're the only one working on this branch, OR
- You've coordinated with your team, OR
- This is your personal fork
The --force-with-lease flag is safer than --force because it fails if someone else pushed in the meantime.
### Understanding Fast-Forward vs Non-Fast-Forward
A fast-forward update is when the remote branch's tip is an ancestor of your local commits. Git can simply move the remote pointer forward:
Before push:
Remote: A - B - C
Local: A - B - C - D - E (fast-forward possible)
After push:
Remote: A - B - C - D - EA non-fast-forward update means the histories have diverged:
Remote: A - B - C - F - G (someone else pushed F and G)
Local: A - B - C - D - E (you have D and E)Git refuses because pushing would lose commits F and G.
### Preventing This Error
1. Pull before you start working: git pull at the start of each session
2. Pull before you push: Make it a habit to run git pull --rebase before pushing
3. Use feature branches: Avoid pushing directly to main/master
4. Communicate with your team: Coordinate when multiple people need to push to the same branch
### When Force Push Is Acceptable
Force pushing is generally safe when:
- Working on your own feature branch that no one else uses
- Cleaning up commits before a pull request review
- Fixing a mistake immediately after pushing (before anyone pulled)
It's dangerous when:
- Working on shared branches (main, develop)
- Other developers have based work on your commits
- CI/CD systems are tracking the branch
### CI/CD Considerations
If your CI/CD pipeline commits back to the repository (version bumps, changelog updates, etc.), this can cause non-fast-forward errors. Solutions:
- Have CI push to a different branch
- Always pull before local development
- Use merge commits instead of rebase in CI
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server