# 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 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`.
3. **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
    ```
    
4. **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.
3. **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.
    
4. **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.