Course contentsShow
← Git & GitHub
Lesson 55 of 963. Branching and TeamworkFree lesson

Resolving Diverged Histories During Pulls

Learners will handle synchronization conflicts that occur when remote changes overlap with local commits by performing manual merges after pulling.

In this lesson, you will learn how to gracefully reconcile your local work with remote updates when both histories have moved in different directions.

Understanding Diverged Histories

Up until now, you’ve likely been "pulling" changes where your local branch was simply behind the remote branch. In those cases, Git performs a "fast-forward" merge. However, a diverged history occurs when you have made new commits locally and someone else has pushed new commits to the remote repository. When you try to git pull, Git realizes it cannot simply move your branch pointer forward because your histories have split.

The Anatomy of a Pull Conflict

When you run git pull, Git attempts to automatically merge the remote changes into your local branch. If the changes overlap in the same lines of code, Git will pause the process and report a conflict.

Think of this like two people editing the same paragraph of a shared document. Git is effectively saying: "I see your changes and I see their changes, but I don’t know which version should be the final one."

Resolving the Divergence

When this happens, your local repository enters a state where the merge is "in progress." You must now handle the situation just as you would a standard merge conflict:

  1. Identify the files: Run git status to see which files contain the diverged code.
  2. Open the files: Look for the conflict markers (<<<<<<<, =======, >>>>>>>).
  3. Manual Resolution: Edit the file to integrate the logic you want, removing the conflict markers in the process.
  4. Finalize: Once the code is corrected, stage the resolved files:
    git add <filename>
    
  5. Complete: Commit the resolution to finalize the merge:
    git commit -m "Resolved diverged history by merging remote updates"
    

By completing this commit, you are effectively "re-joining" the two histories, creating a new merge commit that satisfies both your local progress and the remote updates.

Key Takeaway: Diverged histories are a natural part of team collaboration; resolve them by manually merging the conflicting sections and finalizing the state with a new commit.