Resolving Merge Conflicts with Git
Manually Resolving Merge Conflicts
To resolve a merge conflict manually, follow these steps:
- Open the conflicting file in a text editor.
- Locate the conflict markers (
<<<<<<
, =======
, and >>>>>>>
).
- Decide which changes you want to keep and remove the conflict markers.
- Save the file.
- Add the resolved file to the staging area using
git add <filename>
.
- Commit the resolved conflict using
git commit
.
Here's an example of a conflicting file and how to resolve it:
<<<<<<< HEAD
## LabEx Git Tutorial
=======
## LabEx Git Crash Course
>>>>>>> other-branch
To resolve this, you might decide to keep both the title and the branch name, resulting in:
## LabEx Git Tutorial: Crash Course
Instead of resolving conflicts manually, you can use a merge tool to help visualize and resolve the conflicts. Some popular merge tools include:
- Visual Studio Code: Integrated merge tool
- Beyond Compare: Cross-platform merge tool
- KDiff3: Open-source merge tool
To use a merge tool, configure Git to use your preferred tool with the git config
command:
git config --global merge.tool <tool-name>
git config --global mergetool.<tool-name>.cmd '<tool-command> "$LOCAL" "$REMOTE" "$BASE" "$MERGED"'
Then, when you encounter a merge conflict, run git mergetool
to launch the configured tool and resolve the conflicts.
Aborting a Merge
If you're unable to resolve the merge conflicts or want to start over, you can abort the merge using the git merge --abort
command. This will reset your working directory to the state before the merge began.