Merge conflicts occurs , when there is a parallel development in the code or when there is a change in the branch that must be merged to master. In simple way to tell this is , when people make change in code (either adding lines or deleting lines) in the same file, merge conflicts arise. Because GitHub doesn’t know which is correct, so it seeks our help to solve the problem.
For Example : Peter is working on a cucumber project; he created a Home. Feature file with 3 scenarios and push to master. John is another teammate has pulled the code from master, so now John has Home. Feature file in his workspace and also working on his Signin.feature file . The next day, Peter modified his code, say he deleted one scenario in feature file and updated into master. Now John is pushing his code to master , he is not aware that his Teammate has deleted one scenario. In that case GitHub is confused , because currently in master only 2 scenarios are there in Home. Feature file , but John is pushing his code with old 3 scenario file. This where merge conflict arises, and GitHub is let us know that there is difference in master file which you’re trying to merge.
During such scenarios ,Merge conflicts must be resolved manually.
There are two types of merges. 1. Fast forward merge 2. Three-way merge
Fast forward merge : Fast forward merge is merging happens without disturbing the master.
In the above flowchart the first 2 commits happening in master after that branch has been created. Then next two commits happening in inside the branch. After completing, it has been merged into the master. Until that there is no merge in master . This type of merge is called Fast Forward Merge.
Three-way Merge : When commit happens in both side (i.e., in the master as well as branch)
Three -way Merge (flowchart 2)In the flowchart (2) the first 2 commits happening in master after creating branch, there is 1 commit in master. On the other side there is two commits happening inside the branch. Then there is a merge from branch to the master. This is called Three-way merge. Merge conflicts often arise due to Three-way merge.
Let’s see how to solve merge conflict manually : We can resolve conflict in eclipse or through GitHub.
Scenario 1: Peter — Teammate 1 , is deleting the scenario in file and pushing to master. John is creating new file and pushing to master (note : John is having peter old file too).
Method 1 : Resolve conflict in Eclipse :
Step 1: John pushes the code into his branch and pull from master .
Did you notice there is warning — 1. It shows one file have changes , that need to be committed 2. Other file unmerged from the master because there is a conflict
John tries to merge , let us see what will happen
Step 2 : git merge origin BranchNew / master (from master)
Can you see the error : Merging is not possible because you have unmerged files and unresolved conflict in file.
Step 3: If there is conflict in your project folder looks like this. (Red Mark in folders)
Step 4 : Open the folder ,
Your file will have conflict markers first line <<<< branch name middle line ==== end line >>> master remove all these lines and save it .
Step 5: Now let’s do staging , commit, and push to master
Now John has resolved his conflict and he is successful in pushing his code to master. He directly pushed to master ; he doesn’t need to pull . Because his first command was git pull.
Method 2: Resolve merge conflict in GitHub
Step 1 : Do staging , commit , and push code into your Branch in command prompt. Step 2 : Go to GitHub , you will see your push message on the repo header Step 3 : Click compare & pull request
Step 4 : Pull request window will be opened . It will compare your branch with master. If there is a conflict, you will get a merge “can’t automatically merge”.
Click create pull request (at the bottom).
Step 5 : Shows the list of files which has conflict. Now let’s resolve that. Click resolve conflicts.
Step 6 : Same as eclipse , your file will show conflict markers. Remove the markers and save it
Note : On left corner , you will see how many conflicts they’re in file are.
Step 7: Once conflicts are resolved , you will get commit merge
Step 8 : Now you can merge and create pull again.
Step 9 : Click Confirm merge
You will message “Pull request successfully merged and closed”
Scenario 2 : If one of your Teammate do changes in POM.xml , and you pull from master. Entire project folder shows error. Sometimes you may not see any folder. Let’s see how to resolve that.
Simple solution : Just check your POM.xml , some of the dependency will be reflecting for twice or not in alignment. Now copy and paste the POM.xml from the master which is in the GitHub to your eclipse folder. That’s it your conflict is resolved .
Thanks for Reading….
Happy Learning …!