Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Resolving merge conflicts
📖 Scenario: You are working on a team project using Git. Two team members have made changes to the same file, causing a merge conflict. You need to resolve this conflict to keep the project moving smoothly.
🎯 Goal: Learn how to identify, resolve, and complete a merge conflict in Git using command line tools.
📋 What You'll Learn
Create a Git repository with a file named project.txt containing initial content.
Create a new branch called feature and modify project.txt in this branch.
Switch back to main branch and modify project.txt differently.
Attempt to merge feature branch into main and resolve the merge conflict.
Commit the resolved changes and verify the final content.
💡 Why This Matters
🌍 Real World
Merge conflicts happen often when multiple people work on the same code. Knowing how to resolve them keeps projects moving smoothly.
💼 Career
Developers and DevOps engineers must resolve merge conflicts daily to integrate code changes safely and maintain project stability.
Progress0 / 4 steps
1
Create initial Git repository and file
Initialize a new Git repository and create a file named project.txt with the exact content: Initial project setup. Then add and commit this file with the message Initial commit.
Git
Hint
Use git init to start the repo, echo to create the file, then git add and git commit.
2
Create feature branch and modify file
Create a new branch called feature using git branch feature and switch to it with git checkout feature. Then change the content of project.txt to exactly Feature branch update. Add and commit this change with the message Update in feature branch.
Git
Hint
Use git branch and git checkout to switch branches, then edit the file and commit.
3
Modify main branch and attempt merge
Switch back to the main branch using git checkout main. Change the content of project.txt to exactly Main branch update. Add and commit this change with the message Update in main branch. Then attempt to merge the feature branch into main using git merge feature. This will cause a merge conflict.
Git
Hint
Switch back to main, edit and commit the file, then merge feature to see the conflict.
4
Resolve merge conflict and commit
Open project.txt and edit it to contain exactly Resolved content combining main and feature. Then add the file with git add project.txt and commit the merge resolution with git commit -m "Resolved merge conflict". Finally, print the content of project.txt using cat project.txt to verify the final content.
Git
Hint
After editing the file to resolve the conflict, use git add and git commit. Then use cat to show the file content.
Practice
(1/5)
1. What does a merge conflict in Git mean?
easy
A. Git found changes in the same file that it cannot combine automatically.
B. Git has successfully merged all changes without any issues.
C. Git deleted a file during the merge process.
D. Git created a new branch automatically.
Solution
Step 1: Understand merge conflict meaning
A merge conflict happens when Git sees changes in the same part of a file from different branches and cannot decide which to keep.
Step 2: Identify what Git does in this case
Git stops the merge and marks the conflict in the file for you to fix manually.
Final Answer:
Git found changes in the same file that it cannot combine automatically. -> Option A
Quick Check:
Merge conflict = conflicting changes in same file [OK]
Hint: Merge conflict means manual fix needed for overlapping changes [OK]
Common Mistakes:
Thinking Git merges all changes automatically
Confusing conflict with branch creation
Assuming files are deleted automatically
2. Which Git command is used to mark a conflict as resolved after editing the file?
easy
A. git add <file>
B. git commit -m 'resolve conflict'
C. git merge --continue
D. git checkout --conflict
Solution
Step 1: Identify how to tell Git conflict is fixed
After editing the conflicted file, you must stage it to tell Git the conflict is resolved.
Step 2: Choose the correct command to stage files
The command to stage files is git add <file>.
Final Answer:
git add <file> -> Option A
Quick Check:
Stage resolved file with git add [OK]
Hint: Use git add to mark conflict resolved before commit [OK]
Common Mistakes:
Trying to commit before staging resolved files
Using git merge --continue without staging
Using invalid commands like git checkout --conflict
3. Given this conflict marker in a file after a merge:
<<<<<<< HEAD
Line A
=======
Line B
>>>>>>> feature-branch
What will the file content be after you keep only the changes from the feature-branch and stage the file?
medium
A. Line A
B. Line B
C. <<<<<<< HEAD
Line A
=======
Line B
>>>>>>> feature-branch
D. Line A
Line B
Solution
Step 1: Understand conflict markers
The lines between <<<<<<< HEAD and ======= are from current branch; lines between ======= and >>>>>>> feature-branch are from the other branch.
Step 2: Keep only feature-branch changes
To keep only feature-branch changes, remove the markers and the HEAD section, leaving just 'Line B'.
4. You tried to merge a branch but Git reports conflicts. You edited the files but forgot to stage them before committing. What happens if you run git commit now?
medium
A. Git commits the merge with unresolved conflicts included.
B. Git automatically stages and commits the resolved files.
C. Git refuses to commit and shows an error about unmerged paths.
D. Git aborts the merge and resets to previous state.
Solution
Step 1: Understand commit behavior during merge conflicts
Git requires you to stage resolved files before committing a merge. If files are not staged, Git sees conflicts as unresolved.
Step 2: What happens when committing without staging
Git will refuse to commit and show an error about unmerged paths, preventing incomplete merges.
Final Answer:
Git refuses to commit and shows an error about unmerged paths. -> Option C
Quick Check:
Commit without staging resolved files = error [OK]
Hint: Always git add resolved files before git commit [OK]
Common Mistakes:
Assuming git commit auto-stages files
Thinking Git commits partial merges
Expecting merge to abort automatically
5. You have a file with multiple merge conflicts from two branches. You want to keep all changes from both branches but Git shows conflicts. What is the best way to resolve this?
hard
A. Run git reset --hard to discard all changes and merge again.
B. Use git merge --abort to cancel the merge and try again.
C. Delete the file and create a new one with combined content.
D. Manually edit the file to combine both changes, remove conflict markers, then stage and commit.
Solution
Step 1: Understand the goal to keep all changes
Since you want to keep both branches' changes, you must manually combine them in the file.
Step 2: How to resolve conflicts properly
Edit the file to merge both changes, remove conflict markers, then stage with git add and commit the merge.
Final Answer:
Manually edit the file to combine both changes, remove conflict markers, then stage and commit. -> Option D