Merge Conflict Resolution
What you'll build
A repository where two branches change the same lines of the same file, creating a conflict. You will resolve the conflict manually and complete the merge.
Goals
- Create a merge conflict intentionally
- Read and understand conflict markers (<<<<<<<, =======, >>>>>>>)
- Resolve a conflict by editing the file
- Complete the merge with git add and git commit
Prerequisites
- git branch, checkout, merge (Branch Workflow Simulation)
Steps
- 1
Set up a base file
Create a repo with a config file that both branches will modify differently.
Codemkdir conflict-demo cd conflict-demo git init echo "port=3000" > config.txt git add . git commit -m "Add config with port 3000"Expected result
A repo on main with one commit containing config.txt.
- 2
Create conflicting changes on two branches
Create a feature branch that changes the port to 8080, then switch back to main and change the port to 5000. Both branches modify the same line.
Codegit checkout -b feature/api-port echo "port=8080" > config.txt git commit -am "Change port to 8080 for API" git checkout main echo "port=5000" > config.txt git commit -am "Change port to 5000 for dev"Expected result
Both branches changed the same line to different values.
- 3
Merge and hit the conflict
Merge the feature branch into main. Git will report a merge conflict because both branches changed the same line.
Codegit merge feature/api-portExpected result
Git prints 'CONFLICT (content): Merge conflict in config.txt' and pauses the merge.
Hint
Run git status to see which files have conflicts.
- 4
Examine and resolve the conflict
Open config.txt — you will see conflict markers. The section between <<<<<<< HEAD and ======= is your main branch version; between ======= and >>>>>>> is the feature branch version. Edit the file to keep the correct version (8080) and remove all markers.
Codecat config.txt # You see: # <<<<<<< HEAD # port=5000 # ======= # port=8080 # >>>>>>> feature/api-port # Resolve: keep 8080, remove markers echo "port=8080" > config.txtExpected result
config.txt contains only 'port=8080' with no conflict markers.
- 5
Complete the merge
Stage the resolved file and commit to complete the merge. The conflict is resolved and the merge is recorded in history.
Codegit add config.txt git commit -m "Merge feature/api-port: resolve port conflict, use 8080" git log --onelineExpected result
The merge is complete; git log shows the merge commit.
Try it yourself
What you learned
You created, examined, and resolved a merge conflict from start to finish. Conflicts are not scary — they are just Git asking you to choose between two versions. Read the markers, pick the right version, stage, and commit.
Related Resources
Related Lessons
- Merge Conflicts
Learn conflict markers and resolution strategies.
Related Cheatsheets
Related Errors
- Merge Conflict
Common merge conflict scenarios and fixes.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- Merge Conflict
Common merge conflict scenarios and fixes.