Sobes.tech
Junior

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:

  1. Open the conflicting file in an editor.

  2. Manually edit the file, choosing the necessary parts from both versions or creating a new combination.

  3. Remove the markers <<<<<<<, =======, >>>>>>>.

  4. Save the file.

  5. 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 .
    
  6. 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.