Pushing and Pulling Changes with GitHub

Learn how to push local commits to a remote GitHub repository (`git push`) and pull changes from GitHub to your local machine (`git pull`).


Resolving Conflicts

Learn how to resolve merge conflicts that arise when multiple people edit the same files.

Understanding Merge Conflicts

Merge conflicts happen when Git can't automatically merge changes from different branches. This typically occurs when two or more branches have made changes to the same lines in a file, or when one branch deletes a file that another branch modifies. Git flags these situations and requires you to manually resolve the conflict.

Identifying Conflicts

When a merge conflict occurs, Git will add special markers to the affected file(s) indicating the conflicting changes. These markers look like this:

 <<<<<<< HEAD
// Changes from your current branch (e.g., main)
This is a line that was changed on main.
========
// Changes from the branch you're merging in (e.g., feature/new-feature)
This is a line that was changed on feature/new-feature.
>>>>>>> feature/new-feature 
  • <<<<<<< HEAD: Marks the beginning of the changes from your current branch.
  • ========: Separates your changes from the changes being merged in.
  • >>>>>>> feature/new-feature: Marks the end of the changes from the branch you're merging in. The branch name (e.g., `feature/new-feature`) is also displayed.

Resolving Conflicts Manually

To resolve a merge conflict, you need to edit the file(s) containing the conflict markers. Follow these steps:

  1. Open the file(s) with conflict markers in your text editor.
  2. Carefully examine the conflicting changes. Understand what each side of the conflict is trying to accomplish.
  3. Decide which changes to keep, modify, or combine. Often, you'll need to choose one change over the other, or create a new version that incorporates elements of both.
  4. Remove the conflict markers (<<<<<<< HEAD, ========, and >>>>>>> feature/new-feature) after resolving the conflict.
  5. Save the file.
  6. Stage the resolved file: git add <file_name>
  7. Commit the resolved changes: git commit -m "Resolved merge conflict in <file_name>"

Example

Let's say you have the following conflict in a file named README.md:

 <<<<<<< HEAD
# My Project - Updated on Main
This is the main README for my project.  It was updated on the main branch.
========
# My Project - New Feature
This is the main README for my project.  It includes changes from the new feature branch.
>>>>>>> feature/new-feature 

You might decide to combine the descriptions:

 # My Project
This is the main README for my project. It was updated on the main branch and includes changes from the new feature branch. 

Then, you would stage and commit the changes:

 git add README.md
git commit -m "Resolved merge conflict in README.md" 

Using Git Tools for Conflict Resolution

Git provides tools like git mergetool that can help you visualize and resolve conflicts using a visual diff tool. Configuring and using a mergetool can often make the conflict resolution process easier, especially for complex conflicts.