Dear Readers,
In this article,we will see How to Resolve Merge CONFLICT in Git.
Implementation Steps
- Create a new file “ktexperts-1” and put some content.
- Add and commit the file “ktexperts-1”.
- Switch to branch “Ktexperts-New-Branch”.
- Create a new file with same name “ktexperts-1”.
- Add and commit the file “ktexperts-1”.
- Switch to branch “master”.
- Merge commits from branch “Ktexperts-New-Branch” to branch “master”.
- Resolve Merge Conflicts.
- Push commits from Local Repository to central repository.
- Verify file in central repository.
Git Merge CONFLICT
A merge conflict is an event that occurs when Git is unable to automatically resolve differences in code between two commits.
A merge conflict occurs when two branches you’re trying to merge both changed the same part of the same file.
When this happens, Git won’t be able to figure out which version to use.
If you have a merge conflict on the command line, you cannot push your local changes to GitHub until you resolve the merge conflict locally on your computer. If you try merging branches on the command line that have a merge conflict, you’ll get an error message.
To see the current branch
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * master |
Create a new file “ktexperts-1” and put some content
1 2 3 4 5 6 7 |
[root@ip-172-31-42-20 mumbaigit]# cat > ktexperts-1 Ktexperts [root@ip-172-31-42-20 mumbaigit]# cat ktexperts-1 Ktexperts [root@ip-172-31-42-20 mumbaigit]# ls file1.java file1.txt file2.php file3.java file3.txt ktexperts.java ktexperts.txt file1.php file2.java file2.txt file3.php ktexperts ktexperts.php myfile ktexperts-1 |
To see the status of git
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts-1 nothing added to commit but untracked files present (use "git add" to track) |
Add and commit the file “ktexperts-1”
Adding file to staging area
1 2 3 4 5 6 7 |
[root@ip-172-31-42-20 mumbaigit]# git add . [root@ip-172-31-42-20 mumbaigit]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: ktexperts-1 |
Commit the file into Local Repository
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "commited ktexperts-1 from master" [master ec49bcf] commited ktexperts-1 from master 1 file changed, 1 insertion(+) create mode 100644 ktexperts-1 |
To see the latest commit
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git log -1 commit ec49bcf6c8aaea274b82e2257b3bba19ca68f2e9 (HEAD -> master) Author: Ram <ram@gmail.com> Date: Thu Dec 19 07:12:16 2019 +0000 commited ktexperts-1 from master |
Switch to branch “Ktexperts-New-Branch”
1 2 3 4 5 |
[root@ip-172-31-42-20 mumbaigit]# git checkout Ktexperts-New-Branch Switched to branch 'Ktexperts-New-Branch' [root@ip-172-31-42-20 mumbaigit]# git branch * Ktexperts-New-Branch master |
Create a new file with same name “ktexperts-1”
1 2 3 4 5 6 7 |
[root@ip-172-31-42-20 mumbaigit]# cat > ktexperts-1 It will help to share knowledge [root@ip-172-31-42-20 mumbaigit]# cat ktexperts-1 It will help to share knowledge [root@ip-172-31-42-20 mumbaigit]# ls file1.java file1.txt file2.php file3.java file3.txt ktexperts.java ktexperts.txt file1.php file2.java file2.txt file3.php ktexperts ktexperts.php myfile ktexperts-1 |
To see the status of git
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch Ktexperts-New-Branch Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts-1 nothing added to commit but untracked files present (use "git add" to track) |
Add and commit the file “ktexperts-1”
Adding file to staging area
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git add . To see the status of git [root@ip-172-31-42-20 mumbaigit]# git status On branch Ktexperts-New-Branch Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: ktexperts-1 |
Commit the file into Local Repository
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "commited ktexperts-1 from Ktexperts-New-Branch" [Ktexperts-New-Branch c5498f2] commited ktexperts-1 from Ktexperts-New-Branch 1 file changed, 1 insertion(+) create mode 100644 ktexperts-1 |
To see the latest commit
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git log -1 commit c5498f24f4acd81871635f7c5d8eafc136e5db47 (HEAD -> Ktexperts-New-Branch) Author: Ram <ram@gmail.com> Date: Thu Dec 19 07:20:17 2019 +0000 commited ktexperts-1 from Ktexperts-New-Branch |
Switch to branch “master”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout master Switched to branch 'master' |
To see the branch
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * master |
Merge commits from branch “Ktexperts-New-Branch” to branch “master”
If you have a merge conflict on the command line, you cannot push your local changes to GitHub until you resolve the merge conflict locally on your computer. If you try merging branches on the command line that have a merge conflict, you’ll get an error message.
When the conflicted line is encountered, Git will edit the content of the affected files with visual indicators that mark both sides of the conflicting content. These visual markers are
<<<<<<<
– Conflict marker, the conflict starts after this line.=======
– Divides your changes from the changes in the other branch.>>>>>>>
– End of the conflicted lines.
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git merge Ktexperts-New-Branch Auto-merging ktexperts-1 CONFLICT (add/add): Merge conflict in ktexperts-1 Automatic merge failed; fix conflicts and then commit the result. |
Note
Now when we try to merge our new branch with master, we will create a merge conflict.
Merge conflicts must be resolved before we can continue.
Resolve merge CONFLICT
Open and modify the file “ktexperts-1”
1 2 3 4 5 6 7 8 9 10 11 |
[root@ip-172-31-42-20 mumbaigit]# vi ktexperts-1 <<<<<<< HEAD ktexperts ======= It will help to share knowledge >>>>>>> Ktexperts-New-Branch <strong>Delete Unnecessary things </strong>------------------------- [root@ip-172-31-42-20 mumbaigit]# vi ktexperts-1 ktexperts It will help to share knowledge |
To see the content of file “ktexperts-1”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# cat ktexperts-1 ktexperts It will help to share knowledge |
To see the status of git
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-20 mumbaigit1]# git status On branch Ktexperts-New-Branch 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 added: ktexperts-1 no changes added to commit (use "git add" and/or "git commit -a") |
Add and commit the file “ktexperts-1”
Adding file to staging area
1 2 3 4 5 6 7 8 9 10 11 |
[root@ip-172-31-42-20 mumbaigit1]# git add . To see the status of git [root@ip-172-31-42-20 mumbaigit1]# git status On branch Ktexperts-New-Branch All conflicts fixed but you are still merging. (use "git commit" to conclude merge) Changes to be committed: modified: ktexperts-1 |
Commit changes into Local Repository
1 2 |
[root@ip-172-31-42-20 mumbaigit1]# git commit -m "conflict resolved" [Ktexperts-New-Branch 1510d16] conflict resolved |
To see content of file “ktexperts-1”
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# cat ktexperts-1 It will help to share knowledge Ktexperts |
Push commits from Local Repository to central repository
1 2 3 4 5 6 7 8 9 10 |
[root@ip-172-31-42-20 mumbaigit]# git push origin master Username for 'https://github.com': Ramesh-Ktexperts Password for 'https://Ramesh-Ktexperts@github.com': Counting objects: 36, done. Compressing objects: 100% (23/23), done. Writing objects: 100% (36/36), 3.33 KiB | 851.00 KiB/s, done. Total 36 (delta 6), reused 0 (delta 0) remote: Resolving deltas: 100% (6/6), done. To https://github.com/Ramesh-Ktexperts/centralgit.git * [new branch] master -> master |
Verify file in Central Repository
Go inside your GitHub and hit refresh of your browser.
we can see file”ktexperts-1″and commit message.
To check data of the file
Click on ktexperts-1 to open
We can the content of file “ktexperts-1”.
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
Follow Me
Ramesh’s Linkedin : https://www.linkedin.com/in/ramesh-atchala/
Priya
Best learning platform for devops students
santhi
Its a good article and I got so much information regarding to merge CONFLICT in DevOps.