Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Resolving Merge Conflicts | Working with Branches in Git
Git Essentials
course content

Contenido del Curso

Git Essentials

Git Essentials

1. Introduction to Git
2. More Advanced Interaction
3. Undoing Changes
4. Working with Branches in Git

bookResolving Merge Conflicts

Fixing Conflicts

In the previous chapter, we encountered a merge conflict, so now it’s time to fix it. Let’s first open our branch_learning.txt file with the Vim editor using the following command:

If you don’t have Vim installed, you may use other text editors, such as nano or atom (replace vim with the name of your text editor in the command above). However, we highly recommend you to use Vim since it will be much easier for you to follow along.

Here is our file opened in the Vim editor:

Now, we can see markers indicating the conflicting sections. These markers are represented by arrows and special symbols to highlight the conflicting changes from different branches. The conflict markers are the following:

  • <<<<<<< HEAD: Indicates the beginning of the changes from the current branch (master in our case);
  • =======: Separates the changes from the current branch (HEAD) and the changes from the branch being merged;
  • >>>>>>> feature/new-feature: Marks the end of the changes from the branch being merged.

To fix the conflicts, we can either select the change from the current branch (master), select the change from the branch being merged (feature/new-feature) or manually edit the changes.

Let’s manually edit the changes by combining the changes from both branches following the following steps:

1. Enter Edit Mode

First, we have to enter insert (edit) mode in Vim by pressing i. This allows you to edit the text.

2. Edit the Conflicted Section

Next, we’ll combine the lines from both branches one after the other, so the resulting contents of the file will be as follows:

3. Exit Edit Mode, Save and Exit:

To exit insert mode, we should press the Escape key. Afterward, to save the changes and exit, we’ll type :wq and press the Enter key:

Committing the File

Since we resolved the conflicts let’s now add the branch_learning.txt to the staging area and check the status:

The file was modified and was indeed added to the staging area. We can now proceed to committing it:

Once again, the default text editor is opened (Vim in my case) with the default commit message regarding the merge:

Let’s enter the insert mode in Vim via pressing i and modify this message by adding the following line to it: Kept lines from both branches to specify the way we resolved the conflicts. Here is the complete commit message:

To exit the insert mode, save the changes and exit Vim, we should press the Escape key and then type :wq and press the Enter key.

The commit is successful, and the merge conflict is now resolved.

Let’s now take a look at our commit history as a graph (--graph flag) with one line per commit (--oneline flag):

Here, we can see our latest merge commit and the characteristic shape of a three-way merge.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 6
We're sorry to hear that something went wrong. What happened?
some-alt