0
0
Gitdevops~5 mins

Reading conflict markers in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When two people change the same part of a file in different ways, Git cannot decide which change to keep. It marks the file with conflict markers so you can see both versions and fix the conflict.
When you pull changes from a shared repository and your local file has conflicting edits.
When you merge two branches that changed the same lines differently.
When you rebase your branch onto another branch and conflicts appear.
When you cherry-pick a commit that conflicts with your current code.
When you resolve conflicts manually before completing a merge or rebase.
Commands
This command tries to merge the 'feature-branch' into your current branch. If there are conflicting changes, Git will stop and mark the conflicts in the files.
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.
This command shows the content of the file with conflict markers so you can see the conflicting parts and decide how to fix them.
Terminal
cat example.txt
Expected OutputExpected
<<<<<<< HEAD This is the original line from your branch. ======= This is the conflicting line from feature-branch. >>>>>>> feature-branch
This command 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")
Key Concept

If you remember nothing else from this pattern, remember: conflict markers show exactly where Git found different changes so you can choose which to keep.

Common Mistakes
Ignoring conflict markers and committing the file as is.
This leaves the conflict markers in the code, which breaks the program and confuses others.
Manually edit the file to remove conflict markers and keep the correct code, then add and commit.
Deleting the entire conflicting section without reviewing both versions.
You might lose important changes from either branch.
Carefully compare both sides of the conflict markers and combine or choose the correct changes.
Not running 'git add' after fixing conflicts before committing.
Git will not know the conflict is resolved and will prevent the commit.
After fixing conflicts, run 'git add <file>' to mark the conflict as resolved.
Summary
Run 'git merge' to combine branches; conflicts will stop the merge and mark files.
Open files with conflicts to see markers showing both versions.
Fix conflicts by editing, then add and commit to complete the merge.