---
title: "Your branch and 'origin/main' have diverged: fixing a rejected non-fast-forward push"
description: "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."
url: https://www.danny.engineering/answer/git/your-branch-and-originmain-have-diverged-fixing-a-rejected-non-fast-forward-push
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Git"]
---

# Your branch and 'origin/main' have diverged: fixing a rejected non-fast-forward push

## 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:


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


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


From `git push`:


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


And from `git pull` in recent versions of Git:


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


```bash
git fetch origin
git rebase origin/main

# if there are conflicts, fix the files, then:
git add src/app.ts
git rebase --continue

# changed your mind?
git rebase --abort

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.


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


### Tell git pull what to do by default


Set `pull.rebase` once so `git pull` stops asking.


```bash
# rebase on every pull
git config --global pull.rebase true

# or merge on every pull
git config --global pull.rebase false

# or refuse anything that is not a fast-forward, and decide each time
git config --global pull.ff only
```


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


```bash
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:


```bash
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.

## Related reading

- [git-pull documentation](https://git-scm.com/docs/git-pull)
- [git-rebase documentation](https://git-scm.com/docs/git-rebase)
- [git-push documentation (see --force-with-lease)](https://git-scm.com/docs/git-push)
