Data report"State of code review 2024" is now liveRead the full report

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

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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.

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.
Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

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

Terminal
git status

This will output something like:

Terminal
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.

Before making any decisions on how to merge or rebase, fetch the latest changes from the remote repository:

Terminal
git fetch origin

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

To better understand the changes, compare your local branch with the remote branch:

Terminal
git log --oneline --graph --decorate --all

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

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
    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
    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 for more information.

If there are conflicts during the merge or rebase, Git will pause the operation and ask you to resolve the conflicts. Open the conflicting files and make the necessary changes.

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

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

After merging or rebasing, push your changes to the remote repository:

Terminal
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
git push origin main --force

To prevent frequent divergences, regularly pull changes from origin/main and communicate with your team to coordinate pushes:

Terminal
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.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2