More Git answers

by Danny
Published 2 min read

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:


_10
git pull --rebase
_10
git 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:


_10
Your branch and 'origin/main' have diverged,
_10
and have 1 and 2 different commits each, respectively.

From git push:


_10
! [rejected] main -> main (non-fast-forward)
_10
error: failed to push some refs to 'github.com:your-name/app.git'
_10
hint: Updates were rejected because the tip of your current branch is behind
_10
hint: its remote counterpart.

And from git pull in recent versions of Git:


_10
hint: You have divergent branches and need to specify how to reconcile them.
_10
fatal: 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.


_11
git fetch origin
_11
git rebase origin/main
_11
_11
# if there are conflicts, fix the files, then:
_11
git add src/app.ts
_11
git rebase --continue
_11
_11
# changed your mind?
_11
git rebase --abort
_11
_11
git 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.


_10
git pull --no-rebase
_10
# resolve conflicts if needed, then:
_10
git add .
_10
git commit
_10
git push

Tell git pull what to do by default

Set pull.rebase once so git pull stops asking.


_10
# rebase on every pull
_10
git config --global pull.rebase true
_10
_10
# or merge on every pull
_10
git config --global pull.rebase false
_10
_10
# or refuse anything that is not a fast-forward, and decide each time
_10
git config --global pull.ff only

If you choose rebase, rebase.autoStash lets Git stash uncommitted changes before the rebase and restore them afterwards:


_10
git 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:


_10
git 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 --force on a shared branch such as main. 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 status compares against your last fetched copy of the remote, so it can say you are up to date when you are not. Run git fetch first.
  • 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.
#

Was this resource helpful?