Merge conflicts occur when competing changes are made to the
same line of a file, or when one person edits a file and
another person deletes the same file. A merge conflict is an
event that occurs when Git is unable to automatically resolve
differences in code between two commits.
When all the changes in the code occur on different lines or in
different files, Git will successfully merge commits without
your help. However, when there are conflicting changes on the
same lines, a “merge conflict” occurs because Git doesn’t know
code to keep and which to discard.
We will follow some steps to create merge conflicts and resolve
it.
- Create a new work2 branch
- Change some files in work2 branch and commit changes
- Return to master branch and change some files in same place
- Try to merge work2 branch into master branch
- Resolve conflicts manually (edit files in any editor)
- Stage files with resolved conflicts
- Commit changes
- Git will create merge commit
[varelite1@managed1 repo]$ ls -l code*
-rw-r--r--. 1 varelite1 varelite1 6 May 23 11:42 code1
-rw-r--r--. 1 varelite1 varelite1 6 May 23 11:50 code2
[varelite1@managed1 repo]$ git branch work2
[varelite1@managed1 repo]$ git checkout work2
Switched to branch 'work2'
[varelite1@managed1 repo]$ git branch
master
* work2
[varelite1@managed1 repo]$ vi code1
[varelite1@managed1 repo]$ cat code1
code1
This line has been added in code1 to create merge conflict in work2 branch.
[varelite1@managed1 repo]$ vi code2
[varelite1@managed1 repo]$ cat code2
code2
This line has been added in code2 to create merge conflict in work2 branch.
[varelite1@managed1 repo]$ git add .
[varelite1@managed1 repo]$ git commit -m "code1 and code2 changed"
[work2 c4e5ffd] code1 and code2 changed
2 files changed, 2 insertions(+)
[varelite1@managed1 repo]$ git checkout master
Switched to branch 'master'
[varelite1@managed1 repo]$ git branch
* master
work2
[varelite1@managed1 repo]$ git log
commit 2f379351f27ca75c4336cd869c26a05f46b6fa1f (HEAD -> master, origin/master)
Merge: d3b8d28 f1ad573
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 20:40:05 2024 +0530
Merge branch 'work2' with master a 3 way merge
[varelite1@managed1 repo]$ vi code1
[varelite1@managed1 repo]$ vi code2
[varelite1@managed1 repo]$ git add .
[varelite1@managed1 repo]$ git commit -m "code1 and code2 changed in master branch"
[master 6cc74e8] code1 and code2 changed in master branch
2 files changed, 2 insertions(+)
[varelite1@managed1 repo]$
Below is the merge conflict.
[varelite1@managed1 repo]$ git merge work2
Auto-merging code1
CONFLICT (content): Merge conflict in code1
Auto-merging code2
CONFLICT (content): Merge conflict in code2
Automatic merge failed; fix conflicts and then commit the result.
[varelite1@managed1 repo]$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: code1
both modified: code2
[varelite1@managed1 repo]$ git status -s
UU code1
UU code2
[varelite1@managed1 repo]$ git ls-files -s
100644 47f43aef120505c79eafd2b1bd55e4cb78977f98 1 code1
100644 7663d48f1a71a6ed4f743287f750874f0cd1f6b2 2 code1
100644 4ca4d558479439ba3f46f567752af18e3fd504f2 3 code1
100644 aaf50935d7f7136f32ab7654ca02d3a74f93ed55 1 code2
100644 630f5226c389bb44420670512543e44420bce794 2 code2
100644 7f19fcab4ee91f6416861f4f9aa8afcdfdc2aef3 3 code2
You can see that there three versions of both the files and
each has separate numbers while other has number 0.
Number 1 file is common for both the branches or you can say
that it is working directory before any commits. Number 2 file
which is changed and available in master branch while Number 3
is of the work3 branch. Just cat the file and see what it has
actually.
[varelite1@managed1 repo]$ git cat-file -p 47f43ae
code1
[varelite1@managed1 repo]$ git cat-file -p 7663d48f
code1
This line has been added in code1 to create merge conflict in master branch.
Now you need to check and decide what content you need in the
file. Go and edit the file.
[varelite1@managed1 repo]$ vi code1
[varelite1@managed1 repo]$ vi code2
[varelite1@managed1 repo]$ cat code1
code1
This line has been added in code1 to create merge conflict in master branch.
I need all lines
This line has been added in code1 to create merge conflict in work2 branch.
[varelite1@managed1 repo]$ cat code2
code2
This line has been added in code2 to create merge conflict in master branch.
I need all lines
This line has been added in code2 to create merge conflict in work2 branch.
[varelite1@managed1 repo]$ git add .
[varelite1@managed1 repo]$ git commit -m "Corrected the merge conflict"
[master a64a0aa] Corrected the merge conflict
[varelite1@managed1 repo]$ git log
commit a64a0aabf33bc44aec13c6b37999c3b9c01eed3b (HEAD -> master)
Merge: 6cc74e8 c4e5ffd
Author: varelite1 <varelit1@kb.com>
Date: Fri May 31 11:53:02 2024 +0530
Corrected the merge conflict
commit 6cc74e80ec19e295fc00fa0baf1d0470b3cd67d5
Author: varelite1 <varelit1@kb.com>
Date: Fri May 31 11:41:47 2024 +0530
code1 and code2 changed in master branch
commit c4e5ffdc465d659f995009f042af43a14b5e9bfd (work2)
Author: varelite1 <varelit1@kb.com>
Date: Fri May 31 11:38:33 2024 +0530
code1 and code2 changed
All commits are there now in master branch.