How do you resolve conflicts in Git?
sobes.tech AI
Answer from AI
When performing a pull or rebase, a merge of changes occurs. If Git cannot automatically resolve conflicting lines in files, it marks them with special markers:
<<<<<<< HEAD
line from the current branch
=======
line from the branch being merged
>>>>>>> feature/new-feature
To resolve the conflict:
-
Open the conflicting file in an editor.
-
Manually edit the file, choosing the necessary parts from both versions or creating a new combination.
-
Remove the markers
<<<<<<<,=======,>>>>>>>. -
Save the file.
-
Add the resolved file to the index using
git add:# Add a specific file git add path/to/resolved/file.txt # Add all resolved files git add . -
Commit to complete the merge:
# For merge git commit -m "Resolved merge conflict" # For rebase git rebase --continue
If there are many conflicts or they are complex, you can use merge tools:
git mergetool
This will open a graphical tool to step through conflicts.
In case of difficulties during rebase, you can use:
git rebase --abort: Cancel the rebase and return to the state before it started.git rebase --skip: Skip the current conflicting commit (may be unsafe).
It is important to test the changes after resolving conflicts to ensure code functionality.