# How to resolve the git message "your branch and origin main have diverged"

Kenny DuMez  
Graphite software engineer

* * *

**Note**  
This guide explains this concept in vanilla Git. For Graphite documentation, see our [CLI docs](/content/docs/command-reference?utm_source=guidesCallout/index.html).

When working with Git, you might encounter the error message "your branch and 'origin/main' have diverged." This issue arises when there are commits on your local branch that are not on the `origin/main` branch, and vice versa. This divergence typically occurs after other collaborators have pushed changes to the repository, or when local changes have been made that haven't yet been reconciled with remote changes.

### Understanding where your Git branch diverged  
Before resolving the divergence, it's crucial to understand what it means for branches to diverge. In Git, divergence indicates that the branch you are currently on and the remote branch (in this case, `origin/main`) have different commits after their last common ancestor. This can happen in several scenarios:

- You have made commits locally that haven't been pushed to `origin/main`.
- Someone else has pushed commits to `origin/main` that you don't have locally.
- A combination of both.

### Checking the divergence  
To start, check the status of the divergence by using the command:

Terminal

```bash
git status
```

This will output something like:

Terminal

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

This tells you how many commits are on each side of the divergence.

### Step-by-step resolution  
#### Step 1: Fetch the latest changes  
Before making any decisions on how to merge or rebase, fetch the latest changes from the remote repository:

Terminal

```bash
git fetch origin
```

This command updates your remote-tracking branches but doesn't merge any changes into your local branches.

#### Step 2: Compare the differences  
To better understand the changes, compare your local branch with the remote branch:

Terminal

```bash
git log --oneline --graph --decorate --all
```

This command displays a visual representation of the commit history, showing where the branches diverged.

#### Step 3: Decide on merge or rebase  
You have two main options to reconcile the differences: merging or rebasing. The choice depends on your workflow preferences and the project's collaboration norms.

- **Merging** keeps the history of the separate branches and creates a new commit that combines the changes from both branches.

Terminal

```bash
git merge origin/main
```

This will create a merge commit in your local `main` branch, incorporating changes from both your local branch and `origin/main`.

- **Rebasing** rewrites the commit history by applying your local changes on top of the latest commit from `origin/main`.

Terminal

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

This will reapply your local commits one by one on top of the latest `origin/main` commit, resulting in a cleaner, linear history.

See this in-depth guide on the [differences between `git merge` and `git rebase`](/content/guides/git-merge-rebase-differences-benefits/index.html) for more information.

#### Step 4: Resolve conflicts  
If there are conflicts during the merge or rebase, Git will pause the operation and ask you to [resolve the conflicts](/content/guides/git-rebase-conflicts/index.html). Open the conflicting files and make the necessary changes.

After resolving the conflicts, continue the rebase or mark the conflicts as resolved:

Terminal

```bash
git add .
git rebase --continue  # If you were rebasing
# or
git commit -m "Resolved conflicts"  # If you were merging
```

#### Step 5: Push your changes  
After merging or rebasing, [push your changes](/content/guides/how-to-push-to-github/index.html) to the remote repository:

Terminal

```bash
git push origin main
```

If you have rebased, you might need to use the `--force` option, as the history of your branch has changed:

Terminal

```bash
git push origin main --force
```

### Handling future divergences  
To prevent frequent divergences, regularly [pull changes](/content/guides/how-to-use-git-pull/index.html) from `origin/main` and communicate with your team to coordinate pushes:

Terminal

```bash
git pull origin main --rebase  # This rebases your current branch on top of the fetched branch
```

This ensures that your local branch stays up-to-date with the remote branch, reducing the likelihood of conflicts and divergences.
