Your local branch and the remote both have new commits. Learn what diverged means, how to reconcile it with rebase or merge, and how to set pull.rebase so git pull stops asking.
The short answer
#You have commits the remote does not have, and the remote has commits you do not have. Git will not push until you combine them. For your own work on a branch, the simplest fix is to replay your commits on top of the remote ones, then push:
_10git pull --rebase_10git push
If you prefer to keep a merge commit, use git pull --no-rebase instead.
Why it happens
#You will usually see one of these messages. From git status:
_10Your branch and 'origin/main' have diverged,_10and have 1 and 2 different commits each, respectively.
From git push:
_10! [rejected] main -> main (non-fast-forward)_10error: failed to push some refs to 'github.com:your-name/app.git'_10hint: Updates were rejected because the tip of your current branch is behind_10hint: its remote counterpart.
And from git pull in recent versions of Git:
_10hint: You have divergent branches and need to specify how to reconcile them._10fatal: Need to specify how to reconcile divergent branches.
A push only succeeds on its own when it is a fast-forward: the remote branch can move forward to your commit without losing anything. That is only possible if your branch already contains every commit on the remote. When someone else pushed in the meantime, or you amended or rebased a commit you had already pushed, the two branches have split from a common point and a fast-forward is no longer possible.
Git refuses rather than guessing, because the alternative would be to throw away the remote commits.
How to fix it
#Rebase your commits on top of the remote
This keeps history in a straight line, which is the most common choice for personal work and feature branches.
_11git fetch origin_11git rebase origin/main_11_11# if there are conflicts, fix the files, then:_11git add src/app.ts_11git rebase --continue_11_11# changed your mind?_11git rebase --abort_11_11git push
git pull --rebase does the fetch and rebase in one step.
Merge the remote into your branch
This leaves your existing commits untouched and adds a merge commit that joins the two lines. It is the safer choice when other people have already based work on your commits.
_10git pull --no-rebase_10# resolve conflicts if needed, then:_10git add ._10git commit_10git push
Tell git pull what to do by default
Set pull.rebase once so git pull stops asking.
_10# rebase on every pull_10git config --global pull.rebase true_10_10# or merge on every pull_10git config --global pull.rebase false_10_10# or refuse anything that is not a fast-forward, and decide each time_10git config --global pull.ff only
If you choose rebase, rebase.autoStash lets Git stash uncommitted changes before the rebase and restore them afterwards:
_10git config --global rebase.autoStash true
When you rewrote your own pushed branch
If the branch is yours alone and you diverged on purpose, for example by rebasing a feature branch onto the latest main, overwrite the remote with a guarded force push:
_10git push --force-with-lease
--force-with-lease refuses to push if the remote has commits you have not seen, so you cannot wipe out a teammate's work by accident the way a plain --force can.
Common pitfalls
#- Reaching for
git push --forceon a shared branch such asmain. It deletes the remote commits for everyone. - Rebasing commits other people have already pulled. They will get the same divergence on their machines. Merge instead on shared branches.
- Forgetting to fetch.
git statuscompares against your last fetched copy of the remote, so it can say you are up to date when you are not. Rungit fetchfirst. - Confusing the counts. In "have 1 and 2 different commits each", the first number is your local commits and the second is the remote's.
- Pulling with uncommitted changes and no autostash. Commit or stash them first, or the rebase will refuse to start.
Related reading
#Was this resource helpful?