Resolving Merge Conflicts
Svep för att visa menyn
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
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:
New branch
New line from the master branch
New line from the feature branch
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:
git add branch_learning.txt
git status
The file was modified and successfully added to the staging area. Now proceed to commit it:
git commit
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:
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.
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):
git log --graph --oneline
Here, you can see the latest merge commit along with the characteristic structure of a three-way merge.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal