Handling Merge Conflicts in Git


Handling Merge Conflicts in Git

Merge conflicts occur when Git is unable to automatically reconcile differences between two branches that are being merged. This typically happens when changes in the branches being merged overlap or affect the same lines in the files.

Here’s a step-by-step guide on how to handle merge conflicts:


Steps to Handle Merge Conflicts

1. Initiate the Merge

Start by merging one branch into another. For example:

git checkout main git merge feature/login

If Git detects conflicts during this process, it will halt the merge and notify you.

2. Identify Conflicted Files

After a merge conflict occurs, Git will list the files with conflicts:

git status

Files with conflicts will be marked as "Unmerged paths."

3. Open and Review Conflicted Files

Conflicted files contain conflict markers that indicate the conflicting changes. The markers look like this:

<<<<<<< HEAD Changes from the current branch (e.g., `main`) ======= Changes from the branch being merged (e.g., `feature/login`) >>>>>>> feature/login
  • <<<<<<< HEAD: The section of code from the current branch (the branch you are merging into).
  • =======: The separator between conflicting changes.
  • >>>>>>> <branch-name>: The section of code from the branch being merged.

4. Resolve Conflicts

Edit the file to resolve the conflicts. You need to manually choose which changes to keep or merge the changes from both sections. After editing, remove the conflict markers.

Example Resolution:

# Original conflict <<<<<<< HEAD function greet() { console.log("Hello from main branch!"); } ======= function greet() { console.log("Hello from feature/login branch!"); } >>>>>>> feature/login # Resolved version function greet() { console.log("Hello from both branches!"); }

5. Mark as Resolved

After resolving the conflicts in a file, stage the file to mark it as resolved:

git add <file>

Example:

git add src/app.js

6. Complete the Merge

Once all conflicts are resolved and staged, commit the merge to complete the process:

git commit

Git will automatically create a merge commit with a default commit message, which you can edit if necessary.

Note: If you need to abort the merge and revert to the state before the merge started, you can use:

git merge --abort

This command will cancel the merge and restore your working directory to its previous state.


Summary

  • Merge Conflicts: Occur when Git cannot automatically reconcile changes between branches.
  • Steps to Handle Conflicts:
    1. Initiate the Merge: Start merging branches.
    2. Identify Conflicted Files: Check git status for files with conflicts.
    3. Open and Review Files: Look for conflict markers (<<<<<<<, =======, >>>>>>>).
    4. Resolve Conflicts: Edit files to resolve conflicts and remove markers.
    5. Mark as Resolved: Stage the resolved files using git add.
    6. Complete the Merge: Commit the merge using git commit.
  • Abort Merge: Use git merge --abort if you want to cancel the merge.

Handling merge conflicts carefully is crucial to maintaining a clean and functional codebase, especially in collaborative environments where multiple changes are being made concurrently.