Master Branch and 'Origin/master' Have Diverged, How to 'Undiverge' Branches'?
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 onorigin/master
. - Remote Divergence: The
origin/master
branch has commits that are not on your localmaster
.
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:
Fetch Latest Changes from Remote
First, update your local repository with the latest changes from the remote:
git fetch origin
Rebase Your Local Branch
Rebase your local
master
branch on top oforigin/master
:git rebase origin/master
- This re-applies your local commits on top of the commits in `origin/master`.
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
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:
Fetch Latest Changes from Remote
Update your local repository:
git fetch origin
Merge
origin/master
into Your Local BranchMerge the remote changes into your local
master
:git merge origin/master
- This creates a merge commit combining your local changes with the remote changes.
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.
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
Fetch Latest Changes:
git fetch origin
Rebase Local Branch:
git rebase origin/master
Resolve Conflicts (if any):
git add <resolved-file> git rebase --continue
Push with Force:
git push origin master --force
Example Workflow for Merge
Fetch Latest Changes:
git fetch origin
Merge Remote Changes:
git merge origin/master
Resolve Conflicts (if any):
git add <resolved-file> git commit
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.
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github