Learn why conflicts happen and how to resolve them.
Merge conflicts happen when two people make changes to the same file on GitHub—a common occurrence when you’re working with others. While resolving differences might involve some discussion, merge conflicts don’t have to be scary. This course guides you through the steps to finding the best merge conflict solution, so your team can keep building.
- Who is this for: New developers, new GitHub users, users new to Git, students, managers, teams.
- What you'll learn: What merge conflicts are, how you resolve merge conflicts, how to reduce merge conflicts.
- What you'll build: We'll work with a short Markdown resume file in this course.
- Prerequisites: We recommend taking Introduction to GitHub prior to this course.
- How long: This course is three steps long and takes less than 30 minutes to complete.
- Right-click Start course and open the link in a new tab.
- In the new tab, most of the prompts will automatically fill in for you.
- For owner, choose your personal account or an organization to host the repository.
- We recommend creating a public repository, as private repositories will use Actions minutes.
- Scroll down and click the Create repository button at the bottom of the form.
- After your new repository is created, wait about 20 seconds, then refresh the page. Follow the step-by-step instructions in the new repository's README.
Welcome to "Managing Merge Conflicts"! 👋
What is a merge conflict?: 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 so let's start by creating one.
- Open a new browser tab, and work on the steps in your second tab while you read the instructions in this tab.
- We made a small change to a file in the repository in the
my-resume
branch. - Create a pull request setting
my-resume
as the head branch andmain
as the base branch. You can enterResolving merge conflicts
for the pull request title and body. - Wait about 20 seconds then refresh this page for the next step.
Good start! Now let's look deeper at a merge conflict! 🔍
This can be intimidating, but have no fear, Git is smart when it comes to merging! Git only needs a human to decide how to resolve the conflict. 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.
- Open the pull request that you just created, we created a conflict for you. Have no fear!
- At the bottom of the page, under "This branch has conflicts that must be resolved", click the Resolve conflicts button.
- Look for the highlighted sections that begins with
<<<<<<< my-resume
and ends with>>>>>>> main
. These markers are added by Git to show you the content that is in conflict. - Remove the changes made on the main branch by deleting all of the content below the
=======
and above>>>>>>> main
. - Next, remove the merge conflict markers by deleting the following lines:
<<<<<<< my-resume ======= >>>>>>> main
- With the merge conflict markers removed, click Mark as resolved.
- Finally, click Commit merge.
- Wait about 20 seconds then refresh this page for the next step.
Good job! You've solved a merge conflict! 🎉
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 my-resume
branch. With a reverse merge, only the my-resume
branch is updated. This allows you to test the resolved changes on your branch before you merge it into main
.
Now, let's get a little evil. (It's for educational purposes!)
We went ahead and added a new file called references.md
and pushed that change to main
, without updating your my-resume
branch.
- Browse to the
my-resume
branch. - Click the
Add file
dropdown menu and then onCreate new file
. - Create a file named
references.md
. - Enter some text that conflicts with what we added for
references.md
in themain
branch. - Scroll to the bottom of the page and enter a commit message for your change.
- Click the Commit new file button, making sure the "Commit directly to the
my-resume
branch" option is selected. - Wait about 20 seconds then refresh this page for the next step.
Almost there! ❤️
You can now merge your pull request!
- First, resolve any remaining conflicts in your pull request.
Look back at step one if you need help.
- Click Merge pull request.
- Delete the branch
my-resume
(optional). - Wait about 20 seconds then refresh this page for the next step.
Congratulations friend, you've completed this course!
Here's a recap of all the tasks you've accomplished in your repository:
- You learned why merge conflicts happen.
- You resolved a simple merge conflict.
- You created a merge conflict, and resolved it!
- Make your own Markdown resume site with GitHub Pages! Learn how in our GitHub Pages course.
- We'd love to hear what you thought of this course in our discussion board.
- Take another GitHub Skills course.
- Read the GitHub Getting Started docs.
- To find projects to contribute to, check out GitHub Explore.
Get help: Post in our discussion board • Review the GitHub status page
© 2022 GitHub • Code of Conduct • MIT License