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

bookResolving Merge Conflicts

Fixing Conflicts

In the previous chapter, a merge conflict occurred, so now it's time to fix it. First, open the branch_learning.txt file in the Vim editor using the following command:

Opening Vim

If Vim is not installed, you can use another text editor such as nano or atom by replacing vim with your editor’s name in the command above.
However, it is recommended to use Vim for easier alignment with the steps shown here.

Here is the file opened in the Vim editor:

File with conflicts opened in Vim

Now you can see the conflict markers indicating the conflicting sections.
These markers use arrows and special symbols to highlight differences between branches:

  • <<<<<<< HEAD; marks the start of changes from the current branch (master);
  • =======; separates the changes from the current branch (HEAD) and those from the branch being merged;
  • >>>>>>> feature/new-feature; marks the end of the changes from the branch being merged.

To resolve the conflict, choose one of the following options:

  • Keep the changes from the current branch (master);
  • Keep the changes from the merged branch (feature/new-feature);
  • Manually edit the content.

Manually edit the file by combining the changes from both branches using the following steps:

1. Enter Edit Mode

First, enter insert mode in Vim by pressing i. This enables you to edit the text.

2. Edit the Conflicted Section

Next, combine the lines from both branches sequentially. The resulting content of the file should look as follows:

Resolving the conflict

3. Exit Edit Mode, Save, and Exit

Press the Escape key to exit insert mode. Then type :wq and press Enter to save the changes and exit Vim:

Exiting Vim

Committing the File

Since the conflicts are resolved, add the branch_learning.txt file to the staging area and check the status:

Adding resolved file

The file was modified and successfully added to the staging area. Now proceed to commit it:

Committing the file

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

Default commit message

Enter insert mode in Vim by pressing i, then modify the message by adding the line Kept lines from both branches to describe how the conflicts were resolved.

Here is the complete commit message:

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

Saving the modified message and exiting Vim

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):

Commit history graph

Here, you can see the latest merge commit along with the characteristic structure of a three-way merge.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what a three-way merge is?

How do I interpret the output of `git log --graph --oneline`?

What should I do if I see unexpected results in the commit graph?

Awesome!

Completion rate improved to 3.57

bookResolving Merge Conflicts

Swipe to show menu

Fixing Conflicts

In the previous chapter, a merge conflict occurred, so now it's time to fix it. First, open the branch_learning.txt file in the Vim editor using the following command:

Opening Vim

If Vim is not installed, you can use another text editor such as nano or atom by replacing vim with your editor’s name in the command above.
However, it is recommended to use Vim for easier alignment with the steps shown here.

Here is the file opened in the Vim editor:

File with conflicts opened in Vim

Now you can see the conflict markers indicating the conflicting sections.
These markers use arrows and special symbols to highlight differences between branches:

  • <<<<<<< HEAD; marks the start of changes from the current branch (master);
  • =======; separates the changes from the current branch (HEAD) and those from the branch being merged;
  • >>>>>>> feature/new-feature; marks the end of the changes from the branch being merged.

To resolve the conflict, choose one of the following options:

  • Keep the changes from the current branch (master);
  • Keep the changes from the merged branch (feature/new-feature);
  • Manually edit the content.

Manually edit the file by combining the changes from both branches using the following steps:

1. Enter Edit Mode

First, enter insert mode in Vim by pressing i. This enables you to edit the text.

2. Edit the Conflicted Section

Next, combine the lines from both branches sequentially. The resulting content of the file should look as follows:

Resolving the conflict

3. Exit Edit Mode, Save, and Exit

Press the Escape key to exit insert mode. Then type :wq and press Enter to save the changes and exit Vim:

Exiting Vim

Committing the File

Since the conflicts are resolved, add the branch_learning.txt file to the staging area and check the status:

Adding resolved file

The file was modified and successfully added to the staging area. Now proceed to commit it:

Committing the file

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

Default commit message

Enter insert mode in Vim by pressing i, then modify the message by adding the line Kept lines from both branches to describe how the conflicts were resolved.

Here is the complete commit message:

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

Saving the modified message and exiting Vim

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):

Commit history graph

Here, you can see the latest merge commit along with the characteristic structure of a three-way merge.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6
some-alt