How to Resolve Merge Conflicts in Git Like a Pro

Summarize this article with:
Your terminal shows “CONFLICT” in bold red letters. The merge failed.
Now what?
Learning how to resolve merge conflicts in Git is a skill every developer needs. Whether you’re working solo or collaborating on a team project, conflicts happen when two branches modify the same lines of code.
This guide walks you through the complete conflict resolution process. Step by step. No guesswork.
You’ll learn to identify conflicting files, read Git’s conflict markers, decide which changes to keep, and finalize your merge commit cleanly.
Takes about 5-15 minutes with any text editor.
Let’s fix that conflict.
How to Resolve Merge Conflicts in Git

A merge conflict in Git occurs when two branches modify the same lines in a file, and Git cannot automatically determine which changes to keep.
You need this when pulling updates from a remote repository, merging feature branches into main, or rebasing commits onto another branch.
This guide covers 6 steps requiring 5-15 minutes and basic familiarity with Git commands.
Prerequisites
Before you start, make sure you have these ready:
- Git version 2.x or later installed
- A text editor or IDE for development (VS Code, Vim, Sublime Text)
- Access to terminal, Command Prompt, or Git Bash
- Basic knowledge of version control concepts
- An existing Git repository with conflicting branches
Time estimate: 5-15 minutes per conflict depending on complexity.
Step 1: How Do You Identify a Merge Conflict in Git?
Git displays conflict notifications in your terminal immediately after a failed automatic merge attempt, listing each conflicted file path and marking them as “unmerged.”
What Command Shows Merge Conflicts?
Run git status to see all unmerged paths.
The output shows files under “Unmerged paths” in red text. Look for the phrase “both modified” next to filenames.
Action Steps
- Open your terminal in the repository’s codebase directory
- Run:
git status - Note every file listed under “Unmerged paths”
Expected Output
You will see something like:
“ Unmerged paths: (use "git add <file>..." to mark resolution) both modified: src/app.js both modified: config/settings.json `
Purpose: Knowing exactly which files contain conflicts prevents you from missing any unresolved changes before committing.
Step 2: How Do You Open the Conflicted File?
Open each conflicted file in your text editor to view the conflict markers that Git inserted automatically; these markers show both versions of the conflicting code.
Navigate to the File
Use the file paths from git status output.
Open with your preferred editor: code src/app.js for VS Code, or vim src/app.js for Vim.
Action Steps
- Copy the exact file path from the git status output
- Run: code
or open manually in your IDE
- Search for <<<<<<<
to jump directly to conflict sections
What You Will See
The file contains special markers: <<<<<<< HEAD, =======, and >>>>>>> branch-name.
These divide your current branch changes from the incoming branch changes.
Purpose: Opening files directly lets you examine both code versions side by side before deciding what to keep.
Step 3: How Do You Read Git Conflict Markers?
Conflict markers are special text patterns that separate your local changes (HEAD) from incoming changes, with a divider line between them showing exactly where the code differs.
Understanding Each Marker Section
The <<<<<<< HEAD line starts your current branch version.
Everything between this and ======= is what exists in your local branch right now.
Marker Breakdown
- <<<<<<< HEAD
- Start of your current branch code
- =======
- Separator between the two versions
- >>>>>>> feature-branch
- End marker showing incoming branch name
Example Conflict
` <<<<<<< HEAD const apiUrl = "https://api.production.com"; ======= const apiUrl = "https://api.staging.com"; >>>>>>> feature/new-endpoint `
Purpose: Understanding markers prevents accidental deletion of code and helps you make informed decisions about which changes to keep.
Step 4: How Do You Choose Which Changes to Keep?
Evaluate both code versions in the conflicted file, then manually delete the unwanted lines along with all conflict markers to create the final merged result.
Decision Options
You have three choices: keep your changes (HEAD), accept incoming changes, or combine both versions into something new.
The right choice depends on your Git workflow and what each branch was meant to accomplish.
Action Steps
- Read both code sections between the markers carefully
- Delete the version you do not want (or merge both manually)
- Remove all three marker lines: <<<<<<<
,=======,>>>>>>>
- Save the file
After Editing
Your file should contain clean code with zero conflict markers remaining.
Run a quick syntax check or linting to catch any errors introduced during manual edits.
Purpose: Manual resolution gives you full control over the final merged code rather than accepting automated guesses.
Step 5: How Do You Mark the Conflict as Resolved?
Use git add to stage the resolved file, which tells Git you have finished fixing the conflict and the file is ready for commit.
Stage the Resolved File
Run git add for each file you fixed.
You can also use git add . to stage all resolved files at once if you have fixed everything.
Action Steps
- Run: git add src/app.js
(use your actual filename)
- Verify with: git status
- Confirm files moved from “Unmerged paths” to “Changes to be committed”
Expected Output
` Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: src/app.js `
Purpose: Staging signals to Git that your conflict resolution is complete and ready for the merge commit.
Step 6: How Do You Complete the Merge After Resolving Conflicts?
Run git commit to finalize the merge; Git automatically generates a merge commit message that you can accept or customize.
Finalize the Merge
Simply run git commit without the -m flag to use Git's default merge message.
Or run git commit -m “Resolve merge conflict in app.js” for a custom message.
Action Steps
- Run: git commit
- Accept or edit the default merge message in your editor
- Save and close the editor to complete
After Committing
Check git log to see your new merge commit at the top of the history.
You can now push to your remote repository if needed.
Purpose: The commit permanently records your conflict resolution in the repository’s commit history.
Verification
Confirm all conflicts are resolved and your merge completed successfully before continuing work.
Commands to Verify
- git status
- Should show "nothing to commit, working tree clean"
- git log –oneline -5
- Shows recent commits including your merge
- git diff HEAD~1
- Review what changed in the merge commit
Additional Checks
Run your unit tests to catch any broken functionality from the merge.
Build your project to verify no syntax errors exist in merged files.
Troubleshooting
Issue: Conflict Markers Still Appear After Saving
Solution: Search the file for <<<<<<< patterns; you missed removing some markers. Delete all three marker lines and save again.
Issue: Cannot Stage File After Editing
Solution: Check for remaining conflict markers with grep -r “<<<<<<<” . in your project directory. Fix any found, then run git add again.
Issue: Want to Start Over
Solution: Run git merge –abort to cancel the merge and return to pre-merge state. You can then try merging again or use git rebase as an alternative.
Issue: Accidentally Committed Wrong Resolution
Solution: Use git revert to undo the merge commit, then resolve conflicts again correctly.
Alternative Method: Using a GUI Merge Tool
Visual merge tools provide side-by-side file comparison with point-and-click conflict resolution instead of manual text editing.
Command Line Resolution (This Guide)
- Time: 5-15 minutes
- Complexity: Intermediate
- Best for: Single files, quick fixes, remote servers without GUI
GUI Merge Tool (VS Code, GitKraken, Sourcetree)
- Time: 3-10 minutes
- Complexity: Beginner-friendly
- Best for: Multiple conflicts, complex changes, visual learners
Configure a Merge Tool
Set your preferred tool with git config:
` git config --global merge.tool vscode git config --global mergetool.vscode.cmd 'code --wait $MERGED' `
Then run git mergetool when conflicts occur to launch the visual interface.
Related Processes
After mastering conflict resolution, explore these connected Git operations:
- How to merge two branches in Git – Full branch merging workflow
- What does git stash do – Save uncommitted changes before merging
- How to switch branches in Git – Navigate between branches safely
- How to delete a branch in Git – Clean up after successful merges
- How to clone a Git repository – Set up local copies from remote
- What does git fetch do – Get remote changes without merging
FAQ on How To Resolve Merge Conflicts In Git
What causes a merge conflict in Git?
A merge conflict occurs when two branches modify the same lines in a file. Git cannot automatically determine which version to keep. This commonly happens during collaborative development when multiple team members edit identical code sections simultaneously.
Can I prevent merge conflicts entirely?
Not entirely, but you can reduce them. Pull changes frequently from the remote repository. Communicate with your team about file ownership. Use smaller, focused commits. A solid code review process also helps catch overlapping work early.
What does git merge –abort do?
Running git merge –abort cancels the current merge operation and restores your working directory to its pre-merge state. Use this when you want to start over or need more time to understand the conflicting changes before resolving them.
How do I know which files have conflicts?
Run git status after a failed merge. Conflicted files appear under "Unmerged paths" with "both modified" status. You can also use git diff to see exactly what differs between the two versions.
Should I use a GUI tool or command line for conflicts?
Both work. Command line offers speed and works on any server. GUI tools like VS Code, GitKraken, or Sourcetree provide visual side-by-side comparison. Beginners often find GUI merge tools easier. Experienced developers typically prefer terminal commands.
What happens if I commit without resolving all conflicts?
Git prevents this. You cannot commit while conflict markers remain in staged files. The commit will fail with an error message. You must remove all <<<<<<<, =======, and >>>>>>> markers before Git accepts your commit.
Can I accept all incoming changes at once?
Yes. Run git checkout –theirs to accept all incoming branch changes. Use git checkout –ours to keep your current branch version. These commands skip manual editing but require caution to avoid losing important code.
What is a three-way merge in Git?
A three-way merge compares three versions: your branch, the incoming branch, and their common ancestor commit. Git uses this merge base to determine which changes are new on each side. Most modern merge tools display all three versions during conflict resolution.
Do merge conflicts affect my remote repository?
No. Conflicts exist only in your local working directory until resolved and pushed. Your teammates see nothing until you complete the merge commit and push the changes. The remote branch stays unchanged during your local conflict resolution.
How do I resolve conflicts during a rebase instead of merge?
The process is similar. Fix each conflicted file, remove markers, then run git add followed by git rebase –continue. Rebase applies commits one by one, so you may resolve multiple conflicts sequentially. Use git rebase –abort to cancel.
Conclusion
Knowing how to resolve merge conflicts in Git transforms a frustrating roadblock into a routine task.
You now understand the complete workflow: checking unmerged paths, reading conflict markers, choosing the right changes, staging files, and finalizing your merge commit.
These skills apply whether you use GitHub, GitLab, or Bitbucket. Command line or GUI tools. Solo projects or team collaboration.
Conflicts are normal in distributed version control. They mean your team is actively developing and pushing code.
Practice on a test repository first. Create two branches, edit the same file, and trigger a conflict intentionally.
The more you resolve, the faster it becomes.
Your next merge conflict? You have got this.
- RegEX Cheat Sheet - April 20, 2026
- Top 10 Data Platform Development Companies Rated by Technical Depth, Delivery Track Record, and Fit - April 19, 2026
- iPhone App Permissions Explained: Camera, Location, Microphone - April 18, 2026







