In Git collaborative development, branch merging is a common operation, but “conflicts” are often the most headache-inducing problem for beginners—like two teammates modifying the same line of code in the same file simultaneously. Git doesn’t know which one to prioritize, so it pauses and requires manual resolution. Don’t panic! Master these 3 practical techniques to easily resolve conflicts.
Technique 1: Understand Conflict Markers and Manually Resolve¶
When you run git merge target-branch or git rebase target-branch and encounter Automatic merge failed, conflicts exist. Git marks conflicting areas in files with special syntax; you must first understand these markers before manually resolving.
Step-by-Step:¶
-
Locate Conflict Files:
Rungit statusto see files with conflicts (e.g.,both modified: yourfile.txt). -
Open Conflicts and Identify Markers:
Open the conflict file. You’ll see content like:
<<<<<<< HEAD // Code from your current branch (e.g., dev)
I modified this line in dev branch
======= // Separator
I also modified this line in the feature branch
>>>>>>> feature // Code from the branch to merge (e.g., feature)
- Between
<<<<<<< HEADand=======: Your local branch’s changes (e.g.,dev). - Between
=======and>>>>>>>: Changes from the branch being merged (e.g.,feature).
-
Edit and Retain Correct Code:
- To keep both branches’ changes: Combine the logic (e.g., add merged comments, integrate features).
- To keep only one branch: Delete the unwanted code and retain what you need. -
Mark as Resolved:
After editing, rungit add <conflict-file>(e.g.,git add yourfile.txt) to tell Git the conflict is resolved. -
Continue the Merge Process:
- Formerge: Rungit merge --continue.
- Forrebase: Rungit rebase --continueto complete the merge.
Technique 2: Use Visual Tools for One-Click Resolution¶
Manual editing is error-prone. Most editors or Git clients have built-in “visual conflict resolution tools” for a more intuitive workflow. Take VS Code (popular for beginners) as an example:
Step-by-Step:¶
-
Open Conflict Files:
VS Code automatically highlights conflict regions, showing “local branch code” on the left and “incoming branch code” on the right. -
Quick Selection with Buttons:
- Keep local changes: Click “Accept Current Change” next to left code.
- Keep incoming changes: Click “Accept Incoming Change” next to right code.
- Merge both: Click “Accept Both Changes” (ideal for combining logic).
- Compare details: Click “Compare Changes” to review line-by-line and choose manually. -
Save and Mark as Resolved:
After confirmation, save the file, rungit add <conflict-file>, and continue the merge.
Technique 3: Proactive Preparation to Reduce Conflicts¶
The best way to handle conflicts is to prevent them entirely! Adopt these habits to minimize conflicts:
Habit 1: Pull Latest Code Before Merging¶
Ensure your branch is based on the latest version before merging. For example:
# 1. Switch to the target branch (e.g., master)
git checkout master
# 2. Pull the latest code (avoid outdated local versions)
git pull origin master
# 3. Switch back to your branch (e.g., dev)
git checkout dev
# 4. Merge the target branch (fewer conflicts expected)
git merge master
Habit 2: Small, Frequent Merges¶
Avoid large, infrequent merges. Instead:
- Merge small features into the main branch daily (e.g., git merge feature/xxx).
- Test immediately after merging and fix issues promptly to prevent diverging code.
Summary¶
To resolve Git merge conflicts, focus on:
1. Understanding markers and manual editing: Essential for foundational conflict resolution.
2. Using tools for efficiency: Visual tools (e.g., VS Code) simplify manual work.
3. Proactive prevention: Pull latest code and merge frequently to reduce conflicts at the source.
While initially tricky, practice will make you comfortable—Git only signals “code needs clearer collaboration.” You’ll soon master branch merging!