Black Friday sale: 30% off Pro and Unlimited, until 22 December

“Your branch and origin have diverged”: how to fix it safely

Git says your branch and origin have diverged. See what is on each side, choose rebase or merge, and force-push only the safe way.

You ran git status and Git answered:

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

Nothing is lost. Your branch has a commit the server doesn’t, and the server has a commit you don’t. Git won’t pick a winner for you, and a plain push or pull will stop until you do.

See both sides before you change anything

Run git fetch first, so origin/main is current. Then ask Git for the commits on each side.

git log --oneline --left-right main...@{u}

@{u} is the upstream of your current branch. Three dots mean “everything either side has that the other doesn’t”. A < marks a commit only you have, and > marks a commit only the server has.

< 28750e2 Fix cart rounding
> 18d310a Add coupon field

For only the counts, git rev-list --left-right --count main...@{u} prints 1 1. And git status -sb shows the same as [ahead 1, behind 1].

Read the > lines. They are a teammate’s work that your next move has to respect. Your < lines are yours, and they stay safe in your repository whichever option you choose.

Why git pull --ff-only stops

A fast-forward only works when one side is simply behind the other. Here both moved, so there is nothing to fast-forward to.

git pull --ff-only
fatal: Not possible to fast-forward, aborting.

That refusal is the point of --ff-only: it changes nothing and tells you the branches need a decision. Plain git pull decides for you only when pull.rebase or pull.ff is set. Without either, Git stops with “fatal: Need to specify how to reconcile divergent branches.”

Option 1: rebase your commits on top

Rebase replays your commits after the server’s. The history stays a straight line.

git rebase @{u}
git log --oneline --left-right main...@{u}

The log now shows only your own commit, with a new ID, because it was rewritten.

< 3a3ba94 Fix cart rounding

Git says Your branch is ahead of 'origin/main' by 1 commit. Now a normal git push works. If Git stops on a conflict, fix the files, run git add on them and git rebase --continue. To walk away from it, git rebase --abort puts the branch back as it was.

Rebase rewrites your own unpushed commits. Don’t rebase commits other people already have.

Option 2: merge the server’s work in

Merge keeps both histories and adds one commit that joins them.

git merge @{u}
git push

Use it when the branch is shared and others may have built on your commits. The cost is an extra merge commit in the history.

Force-pushing: only after a rebase, only with a lease

You only need a force push when you rebased commits that were already pushed. The server still has the old versions, so a normal push is rejected.

Never use a plain git push --force. It overwrites whatever is on the server, including a commit a teammate pushed a minute ago. Use the lease instead.

# on a feature branch, after rebasing it
git push --force-with-lease
 + 9d2f6b1...3a3ba94 fix/cart-rounding -> fix/cart-rounding (forced update)

It refuses if the server branch is no longer where you last saw it. But “where you last saw it” means your local origin/fix/cart-rounding, and a background git fetch moves that without you reading what arrived. To pin the lease to the commit you actually reviewed, name it:

git push --force-with-lease=fix/cart-rounding:9d2f6b1

Replace fix/cart-rounding with your branch and 9d2f6b1 with the server’s commit you checked. See how the lease works and where it fails.

Never force-push main, release or any branch that deploys. A pre-push hook and branch protection stop that for you.

Or let your Mac flag it

The same question comes up across every repository you touch. push& (Pushand) is a Mac menu bar app that marks a diverged branch in the repositories you choose, so you see it in the menu before a push is rejected. A normal push or update refuses a diverged branch, and you resolve it in Git as above. See what “ahead” means for the simpler case.

See the features

More Git how-tos