MJPritzl / merge-conflicts

https://lab.github.com/githubtraining/managing-merge-conflicts
MIT License
0 stars 0 forks source link

Welcome! Let's resolve a conflict #1

Closed github-learning-lab[bot] closed 3 years ago

github-learning-lab[bot] commented 3 years ago

Welcome to Managing Merge Conflicts :tada:

Hello, and welcome! If you're here to learn about and practice resolving merge conflicts, you're in the right place.

In this course, you'll learn why merge conflicts happen and solve a few of them. The merge conflicts in this course are simple enough to solve from GitHub.com. But, if you'd prefer, you can solve them using the command line or other local tools.

As an added bonus, the project we are using for this course is a resume hosted on GitHub Pages! So, if you want to keep working after you complete this course, please feel free!

Before starting this course, we recommend completing the Introduction to GitHub Learning Lab first.

Note: You may notice that some branches and pull requests already exist. We'll be using them in later activities in this course.

How merge conflicts happen

A Merge conflict occurs when changes are made to the same part of the same file on two different branches. You usually find out about conflicts in a pull request.

This can be intimidating, but have no fear, Git knows how to handle this! It only needs a human to decide how to resolve the conflict.

Step 1: Resolve a simple conflict

You may merge a lot of pull requests before you encounter your first merge conflict. That’s because Git is smart when it comes to merging. Unless you're paying close attention to other branches, you won't know about conflicts until you create a pull request.

This branch is a great example. In this scenario, two of our friends have been working in this repository. They both created branches, made changes to the _config.yml file, and opened pull requests. One pull request was merged to main without problems, but now the other pull request has a conflict.

The history of main and this branch look something like this:

deviated branches

Because this pull request changes the same lines in the _config.yml file, there is a merge conflict.

Let's help our friends resolve this conflict.

:keyboard: Activity: Resolving your first merge conflict

  1. At the bottom of the page in the "This branch has conflicts that must be resolved" section of the Pull Request, click the Resolve conflicts button.

  2. Look for the highlighted sections that begins with <<<<<<< update-config and ends with >>>>>>> main. These markers are added by Git to show you the content that is in conflict.

  3. Remove the changes made on the main branch by deleting all of the content below the ======= and above >>>>>>> main.

  4. Next, remove the merge conflict markers by deleting the following lines:

    <<<<<<< update-config

    >>>>>>> main
  5. With the merge conflict markers removed, click Mark as resolved.

  6. Finally, click Commit merge.

Sometimes, the best way to resolve a merge conflict is to add content that's from both branches, or even something that isn't on either! This is why Git needs a human to look at the code and make the proper fixes.


Watch below for my response.

github-learning-lab[bot] commented 3 years ago

You did it, @MJPritzl! Nice job resolving the conflict. Most conflicts in your day to day experience should be pretty simple, as in that activity. You may need to discuss the resolution with your peers. If your team is working together and reviewing pull requests, resolving conflicts is easy.

What just happened?

Resolving a conflict doesn't automatically merge the Pull Request in GitHub. Instead, it stores the resolution of the conflict in a merge commit and allows you and your team to keep working.

To resolve a conflict, GitHub performs what is known as a reverse merge. This means that the changes from the main branch were merged into your update-config branch.

With a reverse merge, only the update-config branch is updated. This allows you to test the resolved code on your branch before you merge it into main. The main branch should be treated as production ready, bug-free code.

Step 2: Merge the first resolved pull request

Go ahead and merge this pull request now.

I have already approved this pull request, so if you still see "Review required" in the merge view, try refreshing the page.

:keyboard: Activity: Merge this pull request

  1. Click Merge pull request below.
  2. Click Confirm merge.
  3. Click Delete branch and get ready for your next activity.

Watch below for my response.

github-learning-lab[bot] commented 3 years ago

Good job! You've solved your first merge conflict.


Go to the next pull request now.