Master Branch and 'Origin/master' Have Diverged, How to 'Undiverge' Branches'?

Better Stack Team
Updated on August 12, 2024

When the master branch and origin/master have diverged, it means that both your local master branch and the remote origin/master branch have separate changes that are not shared with each other. To resolve this divergence and synchronize them, you have a few options depending on the situation. Here’s a step-by-step guide to handle this:

Understanding the Divergence

  • Local Divergence: Your local master branch has commits that are not on origin/master.
  • Remote Divergence: The origin/master branch has commits that are not on your local master.

Resolving Divergence

1. Rebase Your Local Branch on Top of origin/master

If you want to keep your local changes but apply them on top of the latest origin/master, follow these steps:

  1. Fetch Latest Changes from Remote

    First, update your local repository with the latest changes from the remote:

     
    git fetch origin
    
  2. Rebase Your Local Branch

    Rebase your local master branch on top of origin/master:

     
    git rebase origin/master
    
 
- This re-applies your local commits on top of the commits in `origin/master`.
  1. Resolve Conflicts (if any)

    If there are conflicts during the rebase, Git will pause and prompt you to resolve them. Resolve conflicts in the files, then:

     
    git add <resolved-file>
    git rebase --continue
    

    If you want to abort the rebase at any time, you can use:

     
    git rebase --abort
    
  2. Push the Rebased Branch

    After a successful rebase, you may need to force push your changes to the remote branch because rebase rewrites commit history:

     
    git push origin master --force
    

    Note: Be cautious with --force, as it will overwrite the remote branch history. Use --force-with-lease for a safer approach:

     
    git push origin master --force-with-lease
    

2. Merge origin/master into Your Local Branch

If you prefer to merge origin/master into your local master, follow these steps:

  1. Fetch Latest Changes from Remote

    Update your local repository:

     
    git fetch origin
    
  2. Merge origin/master into Your Local Branch

    Merge the remote changes into your local master:

     
    git merge origin/master
    
 
- This creates a merge commit combining your local changes with the remote changes.
  1. Resolve Conflicts (if any)

    If there are conflicts during the merge, resolve them in the affected files. After resolving conflicts:

     
    git add <resolved-file>
    git commit
    

    This completes the merge process.

  2. Push the Merged Branch

    Push the merged changes to the remote repository:

     
    git push origin master
    

Choosing Between Rebase and Merge

  • Rebase: Rebase is preferred if you want to maintain a linear project history without merge commits. It rewrites commit history, so be cautious when using it on shared branches.
  • Merge: Merge is preferred if you want to preserve the complete history of changes and are okay with merge commits. It combines changes without rewriting history.

Example Workflow for Rebase

  1. Fetch Latest Changes:

     
    git fetch origin
    
  2. Rebase Local Branch:

     
    git rebase origin/master
    
  3. Resolve Conflicts (if any):

     
    git add <resolved-file>
    git rebase --continue
    
  4. Push with Force:

     
    git push origin master --force
    

Example Workflow for Merge

  1. Fetch Latest Changes:

     
    git fetch origin
    
  2. Merge Remote Changes:

     
    git merge origin/master
    
  3. Resolve Conflicts (if any):

     
    git add <resolved-file>
    git commit
    
  4. Push Changes:

     
    git push origin master
    

Summary

To resolve divergence between your local master and origin/master:

  • Rebase: Reapply your local commits on top of the remote branch using git rebase origin/master, then force push if necessary.
  • Merge: Merge the remote branch into your local branch using git merge origin/master, then push the result.

Choose the method that best fits your workflow and project requirements.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github