| title | Erro de push rejeitado no Git: branches divergentes e como resolver | |||||
|---|---|---|---|---|---|---|
| domain | devops | |||||
| tags |
|
|||||
| language | pt | |||||
| status | published | |||||
| source | https://docs.github.com/en/get-started/using-git/dealing-with-non-fast-forward-errors | |||||
| created | 2026-07-29 | |||||
| confidence | 0.9 | |||||
| verified_date | 2026-07-29 |
This error occurs when Git refuses to push local commits because the remote branch has commits that the local branch does not. The push fails with:
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/usuario/repo.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.
This happens when you and another collaborator modified the same files and the Git history diverged.
Git requires the local history to be a linear superset of the remote history to accept a push. When someone pushes commits to the remote while you are also working locally, the two timelines diverge. Git rejects the push to prevent overwriting someone else's work.
Two approaches exist to resolve this: merge (creates a merge commit) or rebase (rewrites history linearly).
Approach 1: Merge (recommended for beginners)
git pull origin main
git push origin mainIf conflicts occur during merge:
git pull origin main
# Git shows: CONFLICT in arquivo.txt
# Edit the conflicting files manually
git add arquivo.txt
git commit -m "Resolve merge conflicts with origin/main"
git push origin mainApproach 2: Rebase (linear history, more advanced)
git pull --rebase origin main
git push origin mainIf conflicts occur during rebase:
git pull --rebase origin main
# Resolve conflicts in the affected files
git add arquivo.txt
git rebase --continue
git push origin mainTo abort the rebase if something goes wrong:
git rebase --abortApproach 3: Force push (ONLY if you are certain)
git push --force-with-lease--force-with-lease is safer than --force because it checks that the remote has not changed since your last fetch.
- Run
git log --oneline --graph --allto visualize the branch history - Confirm the local branch is ahead of the remote:
git status - Run
git pushand confirm there is no rejection - Verify on GitHub/GitLab that the commits appear correctly
git pullis equivalent togit fetchfollowed bygit merge- Run
git fetchbeforegit pushto check for remote changes without automatically merging - Prefer
--force-with-leaseover--forcein all situations - Agree with your team on whether to use merge or rebase to maintain consistent history
- Set
git config --global pull.rebase trueto use rebase as the default pull strategy - Reference: GitHub docs on non-fast-forward errors