Merge conflicts occur when Git is unable to automatically resolve differences in code between branches. This typically happens when changes are made to the same lines of code in different branches. Understanding how to resolve these conflicts is crucial for maintaining a smooth workflow.
Key Concepts
- Merge Conflict: A situation where Git cannot automatically merge changes due to conflicting modifications in the same part of the code.
- Conflict Markers: Special markers added by Git to indicate the conflicting sections in the files.
- Conflict Resolution: The process of manually editing the conflicting files to resolve the differences.
Steps to Resolve Merge Conflicts
- Identify the Conflict
When a merge conflict occurs, Git will notify you and mark the conflicting files. You can use the following command to see the status:
This will show you which files have conflicts.
- Open the Conflicting Files
Open the files listed in the git status
output. You will see conflict markers like this:
<<<<<<< HEAD // Changes from the current branch ======= # Changes from the branch being merged >>>>>>> branch-name
- Edit the Conflicting Sections
Manually edit the conflicting sections to resolve the differences. Remove the conflict markers and make sure the code is correct. For example:
// Before resolving <<<<<<< HEAD int value = 10; ======= int value = 20; >>>>>>> feature-branch // After resolving int value = 15; // Resolved value
- Mark the Conflict as Resolved
After editing the files, you need to mark the conflicts as resolved. Use the following command:
- Commit the Changes
Finally, commit the resolved changes:
Practical Example
Let's walk through a practical example of resolving a merge conflict.
Scenario
You have two branches: main
and feature
. Both branches have changes in the same file, example.txt
.
Step-by-Step Resolution
-
Merge the
feature
branch intomain
:git checkout main git merge feature
Git will notify you of a conflict in
example.txt
. -
Check the status:
git status
Output:
Unmerged paths: (use "git add <file>..." to mark resolution) both modified: example.txt
-
Open
example.txt
and resolve the conflict:<<<<<<< HEAD Line from main branch ======= Line from feature branch >>>>>>> feature
Edit the file to resolve the conflict:
Resolved line combining both changes
-
Mark the conflict as resolved:
git add example.txt
-
Commit the changes:
git commit -m "Resolved merge conflict in example.txt"
Common Mistakes and Tips
-
Mistake: Forgetting to remove conflict markers.
- Tip: Always double-check the file to ensure all conflict markers are removed.
-
Mistake: Overwriting changes from one branch.
- Tip: Carefully review changes from both branches to ensure important modifications are not lost.
-
Mistake: Not testing the code after resolving conflicts.
- Tip: Always test your code after resolving conflicts to ensure it works as expected.
Exercise
Task
You have two branches, dev
and feature
, both modifying the same line in config.txt
. Resolve the merge conflict.
- Merge
feature
intodev
. - Resolve the conflict in
config.txt
. - Commit the resolved changes.
Solution
-
Merge
feature
intodev
:git checkout dev git merge feature
-
Open
config.txt
and resolve the conflict:<<<<<<< HEAD setting = true ======= setting = false >>>>>>> feature
Edit the file:
setting = true // Decided to keep the setting as true
-
Mark the conflict as resolved:
git add config.txt
-
Commit the changes:
git commit -m "Resolved merge conflict in config.txt"
Conclusion
Resolving merge conflicts is an essential skill for any developer working with Git. By understanding the process and practicing with real-world scenarios, you can ensure smooth collaboration and maintain a clean codebase. In the next module, we will explore branch management techniques to further enhance your Git workflow.
Mastering Git: From Beginner to Advanced
Module 1: Introduction to Git
Module 2: Basic Git Operations
- Creating a Repository
- Cloning a Repository
- Basic Git Workflow
- Staging and Committing Changes
- Viewing Commit History
Module 3: Branching and Merging
- Understanding Branches
- Creating and Switching Branches
- Merging Branches
- Resolving Merge Conflicts
- Branch Management
Module 4: Working with Remote Repositories
- Understanding Remote Repositories
- Adding a Remote Repository
- Fetching and Pulling Changes
- Pushing Changes
- Tracking Branches
Module 5: Advanced Git Operations
Module 6: Git Tools and Techniques
Module 7: Collaboration and Workflow Strategies
- Forking and Pull Requests
- Code Reviews with Git
- Git Flow Workflow
- GitHub Flow
- Continuous Integration with Git
Module 8: Git Best Practices and Tips
- Writing Good Commit Messages
- Keeping a Clean History
- Ignoring Files with .gitignore
- Security Best Practices
- Performance Tips
Module 9: Troubleshooting and Debugging
- Common Git Problems
- Undoing Changes
- Recovering Lost Commits
- Dealing with Corrupted Repositories
- Advanced Debugging Techniques