Checking the Divergence
Before resolving the issue, it’s important to understand how the branches have diverged. Use the following commands to check the state of the branches:
git fetch origin
git log --oneline master ^origin/master
This command shows commits present in the local master but not in origin/master.
git log --oneline origin/master ^master
This command shows commits present in origin/master but not in the local master.
How to Fix “master branch and ‘origin/master’ have diverged”?
When working with Git, you may encounter the “master branch and ‘origin/master’ have diverged” error. This message indicates that the local master branch and the remote origin/master branch have different histories and have gone out of sync. Resolving this divergence involves understanding the state of both branches and merging, rebasing, or resetting appropriately. In this article, we will explore the causes of this error and provide step-by-step solutions.