How to resolve a merge conflict in Git?
A conflict occurs when two branches modify the same lines of a file and Git cannot automatically merge the changes. Here is the step-by-step resolution process.
First, run git status to see conflicted files. Git marks conflicts in files with markers: <<<<<<< HEAD (your changes), ======= (separator), and >>>>>>> branch-name (changes from the other branch).
Open the file in your editor and find the conflict markers. You need to choose which changes to keep: yours, theirs, or combine both. Remove the <<<<<<<, =======, and >>>>>>> markers, leaving only the desired code. Save the file.
After resolving all conflicts in a file, run git add <file> for each resolved file. When all conflicts are resolved, run git commit — Git will create a merge commit automatically with a default message.
For complex conflicts, use visual merge tools: git mergetool launches your configured tool (VS Code, Meld, KDiff3). In VS Code, you can resolve conflicts right in the editor — buttons Accept Current, Accept Incoming, Accept Both.
If you want to cancel the merge and return to the state before it, run git merge --abort. This is useful when there are too many conflicts and it's easier to start over.
Tip: to reduce conflicts, frequently rebase or merge from the main branch into your feature branch, and make small commits that touch fewer files.