master branch and 'origin/master' have diverged, how to 'undiverge' branches'?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Git, a common scenario you might encounter is when your master branch and the origin/master branch have diverged. This means that the history of these branches has changed in ways that are not direct follow-ons from one another. Understanding how to handle this divergence is crucial for maintaining a clean and functional repository.
Understanding Divergence
Divergence occurs when changes are committed to the local master branch that are not in the origin/master branch, and vice versa. This might happen because:
- You have made local commits that haven't been pushed.
- Someone else has pushed commits to the repository that you haven't pulled yet.
This results in a branch history that no longer matches, making it impossible to push or pull without resolving the differences.
Example of Divergence
Suppose you and your teammate are working on the same repository. You both start at the same commit, but you make a commit locally to master, and at the same time, your teammate pushes a different commit to origin/master. The commit history now looks like this:
Here, A is the common ancestor commit, B is your commit, and C is your teammate's commit.
Resolving Divergence
To "undiverge" the branches, you essentially need to integrate the changes from both branches. There are mainly two ways to do this: merging or rebasing.
1. Merging
Merging creates a new "merge commit" in your local repository that ties together the histories of both branches.
If there are no conflicts, this command will merge the changes automatically. If there are conflicts, Git will ask you to resolve them before the merge can complete.
After resolving any conflicts and completing the merge, you would need to push the merge commit back to origin/master:
2. Rebasing
Rebasing rewrites the commit history by applying your local changes on top of the latest commit from origin/master.
This command may also lead to conflicts which you'll need to resolve manually. After fixing any issues and finishing the rebase, push your changes:
Warning: Rebasing and forcing a push can rewrite the history, which might be problematic for other collaborators. Use this with caution.
Choosing Between Merge and Rebase
The choice between merging and rebasing depends on your project’s workflow. Merging preserves the history, while rebasing creates a linear history. It’s often down to project or team preference.
Summary Table
| Action | Command | Result | Considerations |
| Fetch Changes | git fetch origin | Updates your tracking branches (e.g., origin/master). | Safe operation (read only). |
| Merge | git merge origin/master | Creates a new commit that ties together diverged branches. | Preserves history of branches. |
| Rebase | git rebase origin/master | Reapplies your changes on top of the latest upstream commit. | Rewrites history (linearize). |
| Push | git push origin master [--force] | Updates remote branch with local changes. | Force push can overwrite changes. |
Final Recommendations
To avoid divergence:
- Frequently pull changes from
origin/masterif you're working directly onmaster. - Consider working on feature branches rather than directly on
masterto simplify merging and rebasing.
Handling divergence correctly is integral to a collaborative and efficient development process. By understanding and using the tools Git provides, you can seamlessly integrate changes and preserve the integrity of your project history.

