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:
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:
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:
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:
Committing the File
Since the conflicts are resolved, add the branch_learning.txt file to the staging area and check the status:
The file was modified and successfully added to the staging area. Now proceed to commit it:
Once again, the default text editor is opened (Vim in my case) with the default commit message regarding the merge:
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.
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, you can see the latest merge commit along with the characteristic structure of a three-way merge.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
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
Resolving 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:
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:
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:
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:
Committing the File
Since the conflicts are resolved, add the branch_learning.txt file to the staging area and check the status:
The file was modified and successfully added to the staging area. Now proceed to commit it:
Once again, the default text editor is opened (Vim in my case) with the default commit message regarding the merge:
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.
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, you can see the latest merge commit along with the characteristic structure of a three-way merge.
Thanks for your feedback!