Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Resolving Merge Conflicts | Робота з гілками в Git
Основи Git

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

vim branch_learning.txt
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:

New branch
New line from the master branch
New line from the feature branch
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:

git add branch_learning.txt
git status
Adding resolved file

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

git commit
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:

Merge branch 'feature/new-feature'

Kept lines from both branches

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.

Three-way merge

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

git log --graph --oneline
Commit history graph

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

question mark

What is the recommended way to resolve a merge conflict in Git?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 6

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 4. Розділ 6
some-alt