diff --git a/git/git-essentials.md b/git/git-essentials.md index cb84989..fae2ed3 100644 --- a/git/git-essentials.md +++ b/git/git-essentials.md @@ -102,6 +102,53 @@ git push origin

+## How to solve a Merge Conflict + +Merge conflicts occur when competing changes are made to the same line of a file, or when one person edits a file and another person deletes the same file. + +To resolve a merge conflict caused by competing line changes, you need to choose which changes you want to incorporate and make a new commit. + +The merge conflict error will be shown to you when you try to merge two branches and the aforementioned competing changes exist. + +You can solve the conflict like this: + +1. Navigate to the repository with the conflict in your terminal. + +2. Check which files are affected with the git status command. + +```bash +$ git status +> # On branch branch-b +> # You have unmerged paths. +> # (fix conflicts and run "git commit") +> # +> # Unmerged paths: +> # (use "git add ..." to mark resolution) +> # +> # both modified: styleguide.md +> # +> no changes added to commit (use "git add" and/or "git commit -a") +``` + +3. Open the conflicted file in a text editor. + +4. To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<<. When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD. Next, you'll see =======, which divides your changes from the changes in the other branch, followed by >>>>>>> BRANCH-NAME. In this example, one person wrote "open an issue" in the base or HEAD branch and another person wrote "ask your question in IRC" in the compare branch or branch-a. + +```bash +If you have questions, please +<<<<<<< HEAD +open an issue +======= +ask your question in IRC. +>>>>>>> branch-a +``` + +5. Decide if you want to keep only your branch's changes, keep only the other branch's changes, or make a brand new change, which may incorporate changes from both branches. Delete the conflict markers <<<<<<<, =======, >>>>>>> and make the changes you want in the final merge. + +6. Add and commit your changes. + +Now you can push your changes to your remote repository. + ## Create a Pull Request Once you pushed your working branch, you need to make a pull request against `master`. by visiting the repo's GitHub page and clicking on the green `Compare and pull request` button: