master branch and 'origin/master' have diverged, how to 'undiverge' branches'?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding Divergence between master and origin/master
In Git, you may encounter a situation where your local master branch and the remote origin/master appear to have diverged. This usually happens when changes have been made to both branches independently. Before resolving this divergence, it's vital to comprehend the mechanisms that lead to such a situation and efficiently reconcile the branches.
Reasons for Divergence
Understanding why divergence occurs helps in selecting the appropriate strategy for resolution.
- Local Commits: You have committed changes to the
masterbranch on your local repository. - Remote Changes: Changes were pushed to the
origin/masterby someone else, or perhaps by you from a different machine. - Branch Checkout Misunderstandings: Sometimes, developers accidentally commit changes to
masterinstead of a feature branch.
Identifying Divergence
To check the status and see if there is divergence, you can run:
If there's a divergence, you might see something like:
Resolving Divergence
Several approaches exist to resolve divergence based on the scenario and goals:
1. Rebasing
Rebasing is a powerful tool that helps to maintain a linear project history by moving your local changes on top of the latest changes from the remote:
Pros:
- Makes history cleaner.
- Avoids unnecessary merge commits.
Cons:
- Can be complex if there are conflicting changes, requiring resolution.
- Modifies commit history, potentially problematic for shared branches.
2. Merging
A straightforward approach that creates a merge commit to combine histories:
Pros:
- Simple process.
- Retains complete history and context.
Cons:
- Introduces additional merge commits, potentially cluttering the history.
3. Force Pushing
Sometimes, you might want to force your local changes onto the remote. This should be used sparingly, as it overwrites remote history:
Pros:
- Quickly resolves conflicts by discarding remote history.
Cons:
- Risky, as it erases previous history and can disrupt other collaborators' work.
Additional Considerations
- Conflict Resolution: Whether you rebase or merge, resolving conflicts—if any—will be a necessary step. Conflicts occur when the same line in a file is modified in competing branches.
- Collaboration and Communication: When dealing with remote repositories, ensure effective communication with your team to avoid overwriting valuable work.
- Backup: Before performing operations like rebasing, especially involving force push or master branches, consider taking a temporary branch to back up your current
masterin case of unintended outcomes.
Key Points Summary
| Concept | Pros | Cons |
| Rebase | Cleaner history, avoids merge commits | Complex with conflicts, modifies history |
| Merge | Keeps complete history, simple process | Can clutter history with merge commits |
| Force Push | Quickly resolves, gives local priority | Risky, can disrupt collaborators, erases previous remote history |
| Conflict Resolution | Necessary for resolving commit discrepancies | Requires manual intervention |
Understanding and choosing the correct method to resolve divergence between master and origin/master can safeguard the integrity of your project’s history and collaborate effectively in a team environment. Always ensure your strategy aligns with your project's workflow and team guidelines.
Related reading
- Maven and Spring Boot - non resolvable parent pom - repo.spring.io Unknown host
- Meaning of Git checkout double dashes
- Meaning of the GitHub message push declined due to email privacy restrictions
- Merge development branch with master
- matplotlib error - no module named tkinter
- Maven does not find JUnit tests to run
- merge one local branch into another local branch
- Merge Replication and the Performance Impact on the controller SQL server
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.