How do you resolve a merge conflict in Git?
A merge conflict occurs when Git cannot automatically integrate changes from different branches into a single, unified commit. This typically happens when two branches modify the same part of the same file in different ways, and Git doesn't know which change to keep. Resolving conflicts involves manually reviewing the conflicting sections and deciding which changes to incorporate.
Identifying a Merge Conflict
When a merge conflict occurs, Git will stop the merge process and inform you about the conflicts. You'll see a message like 'Automatic merge failed; fix conflicts and then commit the result.' Your terminal prompt might also indicate a 'MERGING' state.
Steps to Resolve a Merge Conflict
- Identify the files that have conflicts using
git status. - Open each conflicted file in your text editor.
- Locate the conflict markers and manually edit the file to resolve the differences.
- Stage the resolved files using
git add <filename>. - Commit the changes to complete the merge.
Step 1: Identify Conflicted Files
After Git reports a conflict, run git status to see which files are in an 'unmerged' or 'both modified' state. These are the files you need to fix.
git status
Step 2: Open and Understand Conflict Markers
When you open a conflicted file in your text editor, you'll see special markers that Git inserts to highlight the conflicting sections. These markers are:
<<<<<<< HEAD: Marks the beginning of the changes from your current branch (where HEAD points).=======: Separates the changes from the two branches.>>>>>>> branch-name(or commit hash): Marks the end of the changes from the branch you are merging into your current branch (e.g.,feature-branchor the commit ID of the incoming change).
Everything between <<<<<<< HEAD and ======= represents your changes on the current branch. Everything between ======= and >>>>>>> branch-name represents the incoming changes from the other branch.
Step 3: Manually Edit the File(s)
Carefully edit the file to remove the conflict markers and combine the code as desired. You can keep your changes, keep the incoming changes, or combine parts of both to create a new, unified version. Ensure the final content is correct and no conflict markers remain.
Step 4: Stage the Resolved File(s)
Once you've manually resolved the conflicts in a file and removed all markers, you need to tell Git that the file is fixed. Do this by staging the file.
git add <filename>
Repeat this for all conflicted files.
Step 5: Commit the Resolution
After all conflicted files have been staged, commit the merge. Git will pre-populate a commit message for the merge, typically including details about the branches merged and the conflicts resolved. You can accept this message or modify it if needed.
git commit
Once the commit is made, the merge conflict is resolved, and the merge process is complete.
Useful Git Commands for Conflicts
git mergetool: Launches an external merge tool (like KDiff3, Meld, VS Code) to help you resolve conflicts visually. You might need to configure your preferred tool first.git log --merge: Shows a log of commits that are involved in the current merge conflict, which can help you understand the history leading to the conflict.git diff: Shows the current changes in your working directory, highlighting the conflict markers and uncommitted changes.git diff --base <filename>: Shows the differences between the conflicted file and its common ancestor version, which can be useful for understanding the original state before changes were made.git reset --hard HEAD: *CAUTION*: Aborts the merge and reverts your working directory to the state *before* the merge attempt. All local uncommitted changes (including those for conflict resolution) will be lost.git merge --abort: A safer way to abort the merge operation. It reverts to the state before the merge was attempted, but generally preserves any local uncommitted changes you might have made before starting the merge.