Merge Conflicts in Git Tutorial

beginner
6 min

Merge Conflicts in Git Tutorial

Welcome to our comprehensive guide on Merge Conflicts in Git! Let's dive into understanding this crucial aspect of Git, a powerful version control system, together.

Understanding Merge Conflicts šŸ’”

Merge conflicts occur when Git cannot automatically combine changes from two or more branches because those changes overlap in the same files. They are a common issue when collaborating on a project or when merging feature branches into the main branch.

šŸ“ Note: Merge conflicts are not errors, but rather a sign that Git needs your help to resolve the differences between your branches.

Resolving Merge Conflicts āœ…

To resolve a merge conflict, Git marks the conflicting files with error messages and requires you to manually edit those files to resolve the differences. Here's a simple step-by-step guide:

  1. Check for conflicts: After attempting a merge, Git will let you know if there are any conflicts. You can also check for conflicts using the command git status.

  2. Resolve conflicts manually: Open the conflicting files and look for sections marked with <<<<<<<, =======, and >>>>>>>. These sections represent the conflicting changes. You'll need to decide which changes to keep and which to discard.

  3. Save and stage changes: Once you've resolved the conflicts, save the changes and stage them using the commands git add <file> or git add . (to stage all changes).

  4. Commit the resolved merge: Finally, commit the resolved merge using the command git commit -m "Resolved merge conflict".

Real-world Example šŸŽÆ

Let's consider a simple project where we have a README.md file. One branch has updated the title, while another branch has updated the introduction.

bash
$ git checkout feature-branch-1 $ git checkout -b feature-branch-2 $ echo "Updated Title" > README.md $ git add . && git commit -m "Update Title" $ echo "Updated Introduction" > README.md $ git add . && git commit -m "Update Introduction" $ git checkout main $ git merge feature-branch-1 $ git merge feature-branch-2

Now, Git will show a conflict because both branches have updated the same file. You'll need to manually resolve the conflict by choosing which changes to keep.

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

Practice and Quiz šŸ“

Quick Quiz
Question 1 of 1

Which Git command shows if there are any conflicts during a merge?

bash
$ git init # Initialize a new Git repository $ echo "# Sample Project" > README.md $ git add . && git commit -m "Initial commit" $ git checkout -b feature-branch $ echo "Added a new section" >> README.md $ git add . && git commit -m "Added new section" $ git checkout main $ git merge feature-branch

Now, you'll see a conflict in the README.md file. Resolve the conflict, stage and commit the changes, and you're done!

Remember, merge conflicts are a part of the collaborative development process. With practice, they'll become less daunting and more manageable. Happy coding! šŸ’»šŸŽ‰