Bird
Raised Fist0
Gitdevops~7 mins

Resolving merge conflicts in Git - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

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
Introduction
When two people change the same part of a file in different ways, Git cannot decide which change to keep. This is called a merge conflict. You need to fix these conflicts manually to combine the changes.
When you try to merge a feature branch into the main branch and both have changed the same lines in a file.
When you pull changes from a remote repository and your local changes conflict with the incoming ones.
When you rebase your branch onto another branch and conflicts appear in the process.
When multiple team members edit the same file and push their changes at different times.
When you want to combine changes from two branches but Git cannot automatically merge them.
Commands
This command tries to merge the 'feature-branch' into your current branch. If there are conflicts, Git will stop and tell you which files need fixing.
Terminal
git merge feature-branch
Expected OutputExpected
Auto-merging example.txt CONFLICT (content): Merge conflict in example.txt Automatic merge failed; fix conflicts and then commit the result.
Shows which files have conflicts and need to be fixed before you can complete the merge.
Terminal
git status
Expected OutputExpected
On branch main You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: example.txt no changes added to commit (use "git add" and/or "git commit -a")
Open the conflicted file in a text editor to manually fix the conflict by choosing which changes to keep or combining them.
Terminal
nano example.txt
Expected OutputExpected
No output (command runs silently)
Mark the conflict as resolved by adding the fixed file to the staging area.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
Complete the merge by committing the resolved changes with a message explaining the fix.
Terminal
git commit -m "Resolve merge conflict in example.txt"
Expected OutputExpected
[main 1a2b3c4] Resolve merge conflict in example.txt
-m - Add a commit message inline
Key Concept

If you remember nothing else from this pattern, remember: you must manually fix conflicting files and then add and commit them to complete the merge.

Common Mistakes
Trying to commit before resolving conflicts and adding files.
Git will not allow the commit because conflicts are still present and unresolved.
Open conflicted files, fix conflicts, then use 'git add' before committing.
Ignoring conflict markers in files and committing them as is.
The conflict markers will remain in the code, causing errors or broken code.
Remove conflict markers and ensure the file content is correct before adding and committing.
Using 'git merge --abort' without trying to fix conflicts when you want to keep changes.
This cancels the merge and discards the merge attempt, losing the chance to combine changes.
Manually resolve conflicts instead of aborting if you want to keep both changes.
Summary
Run 'git merge' to combine branches; Git will stop if conflicts occur.
Use 'git status' to see which files have conflicts.
Manually edit conflicted files to fix conflicts, then 'git add' them.
Finish by committing the resolved files to complete the merge.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Git found changes in the same file that it cannot combine automatically. -> Option A
  4. 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

  1. 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.
  2. Step 2: Choose the correct command to stage files

    The command to stage files is git add <file>.
  3. Final Answer:

    git add <file> -> Option A
  4. 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

  1. Step 1: Understand conflict markers

    The lines between <<<<<<< HEAD and ======= are from current branch; lines between ======= and >>>>>>> feature-branch are from the other branch.
  2. Step 2: Keep only feature-branch changes

    To keep only feature-branch changes, remove the markers and the HEAD section, leaving just 'Line B'.
  3. Final Answer:

    Line B -> Option B
  4. Quick Check:

    Keep feature-branch changes = Line B [OK]
Hint: Remove conflict markers, keep desired lines, then git add [OK]
Common Mistakes:
  • Leaving conflict markers in file
  • Keeping both changes without cleaning markers
  • Confusing which side is which branch
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

  1. 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.
  2. Step 2: What happens when committing without staging

    Git will refuse to commit and show an error about unmerged paths, preventing incomplete merges.
  3. Final Answer:

    Git refuses to commit and shows an error about unmerged paths. -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Manually edit the file to combine both changes, remove conflict markers, then stage and commit. -> Option D
  4. Quick Check:

    Manual merge + stage + commit = keep all changes [OK]
Hint: Edit conflicts to combine changes, then git add and commit [OK]
Common Mistakes:
  • Aborting merge instead of resolving
  • Deleting files losing changes
  • Resetting hard losing all work