How to Resolve Merge Conflicts in Git Without Losing Code
Quick Diagnostics
A merge conflict in Git occurs when two people modify the same lines of a file in different branches, or when one of them deletes a file that the other is trying to edit. Unable to automatically decide which changes to prioritize, Git halts the process and marks the conflicted files.
Step-by-Step Solution
Step 1: Locate the files marked as conflicted
Upon a failed git merge or git pull, Git will show you a list of marked files. You can check the current status of your changes by running:
git status
(Look for files under the "Both modified" label).
Step 2: Interpret Git's conflict markers
Open the conflicted file with your code editor. You will find visual markers introduced by Git that separate the changes from your branches:
<<<<<<< HEAD
// This is your local code (branch you are currently on)
const API_URL = "https://local-api.test";
=======
// This is the remote code or the code from the branch you are trying to merge
const API_URL = "https://production-api.com";
>>>>>>> main
-
1
Step 1
Decide which line to keep or edit the area to merge both ideas. -
2
Step 2
Remove all conflict markers (<<<<<<<,=======,>>>>>>>).
Step 3: Commit the resolution and resume the merge
Once the files are corrected, add them and finalize the Git transaction via the command line:
# Add the clean, edited files
git add nombre-del-archivo.js
# Finalize the merge by writing a descriptive message
git commit -m "Resolve merge conflict in api configuration"
(Note: If the conflict is too complex and you want to return to the original state of your branches before attempting to resolve it, abort the operation safely by typing git merge --abort or git rebase --abort).
Prevention Advice
Recommended security practices:
- Develop the habit of pulling changes and merging into your main branch frequently (
git pull origin main). Keeping isolated local branches for weeks drastically increases the probability that the same sections of code will be modified by other team members, complicating the resolution of logical dependencies and the integrity of your source code when merging.