Git - Merge Conflicts



Merging Branches in Git

Merging is the process of integrating changes from one branch into another. After developing a feature or fixing a bug on a separate branch, you typically merge that branch back into the main branch to include those changes in the main codebase.

Handling Merge Conflicts

Branch merging doesn't always go as planned. Git experiences a merge conflict if the same part of the same file was edited differently by both auth-module branch and the bugfix branch.

This is how it appears:

$ git merge auth-module
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Git reports the conflict in the file `index.html` and halts the merge process.

It waits for us to manually resolve the conflict by editing the file rather than automatically creating a new merging commit.

We can use git status to see which files are at conflict:

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add ..." to mark resolution)

    both modified:      index.html

no changes added to commit (use "git add" and/or "git commit -a")

Git labels the impacted files as unmerged and adds conflict markers to the file to identify the conflicting portions when it finds conflicting changes during a merge.

A conflicting part in `index.html` might look something like this:

>>>>>> feature/update-header:index.html

In the above example:

  • The conflict shows that the <h1> tag in the header section of index.html has been modified inconsistently by the feature/update-header branch and the master branch (HEAD).

  • We must manually edit the file to decide which changes to keep and how to merge them in order to resolve this dispute.

  • For instance, we may choose to combine the modifications by modifying index.html to include both concepts:

<header>
  <h1>Welcome to our Website</h1>
</header>

We should delete the conflict markers (\\\>>>>>>) from the file after resolving the conflict in index.html.

Next, use git add to stage the file and mark it as resolved in Git:

$ git add index.html

Next, commit the merge after all conflicting files have been staged:

$ git commit

We can use git mergetool to start a visual merge tool that helps with conflict resolution if we would rather work with a graphical tool:

$ git mergetool
This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc3 codecompare vimdiff emerge
Merging:
index.html

Normal merge conflict for 'index.html':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (opendiff):
  • Once we use the specified tool to resolve conflicts and close the window, Git asks us to verify that the merge was successful.

  • Git automatically stages the resolved file and marks it as resolved for us if confirmation is received.

  • By running git status once more, we can confirm the state of conflicts:

$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

    modified:   index.html

Using git commit will complete the merging commit once its made sure all conflicts are handled and staged.

Usually, the default commit message looks something like this:

    Merge branch 'feature/auth-module'

Conflicts:
    index.html

# It seems like you're committing a merge.
# If this isn't correct, please remove the file
# .git/MERGE_HEAD and try again.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# All conflicts fixed but you are still merging.
#
# Changes to be committed:
#   modified:   index.html
Advertisements