Resolve Merge Conflict in The GitHub Repository — Git Case Study 2

Visal Tyagi
DevOps-Guides
Published in
10 min readJun 3, 2024

--

Problem Statement:

You work for Zendrix Software & Co. You have been assigned the task of updating the master branch of their Git repository with all the features from the feature branches.

Following is the GitHub account:

https://github.com/devops-intellipaat/merge-conflict.git

Consider:

● Feature1 branch to be a public branch

● Feature2 branch to be a private branch

The company relies on a monolithic architecture and for now all the code resides in one file “main.c”.

The respective features have been added in the feature branches for main.c. Meanwhile, a security patch was made to the master branch, and now feature1 and feature2 branches are behind from master by 1 commit.

Tasks to Be Performed:

1. Update Feature1 and Feature2 branch with the Security Patch

2. Apply changes of Feature1 and Feature2 branches on master

3. Finally push all the branches to GitHub

For solving this, please fork the repository to your GitHub account and then work. As a solution, please submit your GitHub’s repository link

Note: Here, Security Patch means software & operating system updates that aims to fix security vulnerabilities in a program or product.

Before moving this case study solution, if you have not checked the Git assignments, please check out these Git Assignment Solutions:

Create The Files & Push them to Git Hub Repository — Git Assignment 1

Create the Files in Separate Branches And Perform Stash & Unstash Operations — Git Assignment 2

Create & Delete the Branches in Git Hub from Local & Remote Repository — Git Assignment 3

Create Two Branches & Merge These Branches to the Master Branch — Git Assignment 4

Create a Git Workflow Architecture & Use Hotfix to Push a File — Git Assignment 5

Suggested a Git Workflow Architecture to Manage the Product Release — Git Case Study 1

Check the Git Hub Repository for this Assignment to Copy the Commands:

Problem (1) & (2) Integrated Solution:

Update Feature1 and Feature2 branch with the Security Patch

Apply changes of Feature1 and Feature2 branches on the master

Git Merge-Conflict Case Study
Git Merge-Conflict Case Study

Follow these steps:

A. Create an EC2 Instance to Perform the Case Study

Step 1: Step 1: Go to the “Services” section & search “EC2” here. Put the cursor over “EC2” & click on “Instances” here.

Go to the EC2 Instance
Go to the EC2 Instance

Step 2: Click on “Launch Instances”.

Click on Launch Instances
Click on Launch Instances

Step 3: Choose “Name” as “Git-Case-Study-2” in the “Name and tags” section.

Write Instance Name Here
Write Instance Name Here

Step 4: Choose “AMI” as “ubuntu”.

Choose “Ubuntu AMI”
Choose “Ubuntu AMI”

Step 5: Choose “Instance type” as “t2.micro”.

Choose the Instance type
Choose the Instance type

Step 6: Choose “key pair (name) — required” as “Demo”.

Choose a Key pair name
Choose a key pair name

Step 7: In the “Network Settings”, choose “Select existing security group” in the “Firewall (security groups)” & while choosing the “Common security groups” as “launch-wizard-9”.

Select the Existing Security Group
Select the Existing Security Group

Step 8: Click on “Launch Instance” in the “Summary” section.

Click on “Launch Instance”
Click on “Launch Instance”

Step 9: The “Instance” will be successfully launched, click on “hyperlink (i-0f7c6e67b4674acfe).

Click on the “Hyperlink”
Click on the “Hyperlink”

Step 10: The “Instance [Git-Case-Study-2]” will be in the “Running” State.

Running Instance
Running Instance

Step 11: Select the Instance & Click on “Connect”.

Select the Instance
Select the Instance

Step 12: Again, click on “Connect”.

Click on “Connect”
Click on “Connect”

Step 13: First, update the machine using the below-given command:

sudo apt-get update
Update the Machine
Update the Machine

Step 14: The machine will be successfully updated.

Machine Updated
Machine Updated

B. Fork the Given GitHub Repository from the Problem Statement

Step 1: First, Sign in to the “Git Hub” account. Go to the “github.com” homepage & click on “Sign in”.

Sign into GitHub Account
Sign into GitHub Account

Step 2: Click on “Sign-in”.

GitHub Username and Password
GitHub Username and Password

Step 3: Your “Git Hub Account” has been successfully logged in.

GitHub Account
GitHub Account

Step 4: Copy the given link from the above problem statement & paste it into the browser address bar. The given “Git Hub” repository will be successfully opened.

GitHub Repository
GitHub Repository

Step 5: Fork this repository using the “fork” button.

Fork the Repository
Fork the Repository

Step 6: Put the “merge-conflict” in the description & select the given option: Copy the “master” branch only.

Create a Fork
Create a Fork

Click on “Create fork”.

Step 7: The given repository “merge-conflict” has been successfully forked.

Repository Successfully Forked
Repository Successfully Forked

C. Clone the Given GitHub Repository on Git Hub Local

Step 1: Use the below-given command to clone the repository:

git clone https://github.com/visaltyagi/merge-conflict.git
Clone the Repository
Clone the Repository

Step 2: The repository will be successfully cloned.

Repository Cloned Successfully
Repository Cloned Successfully

D. Check All Branches

Step 1: Run the below-given command here & it will show the “merge-conflict” folder.

ls  

Go to the “merge-conflict” folder using the below-given command:

cd merge-conflict
Go to the “merge-conflict” folder
Go to the “merge-conflict” folder

Step 2: Type the below-given command to check the branch:

git branch

Only the “master” branch will be shown.

The “master” branch shown
The “master” branch shown

Step 3: Run these below-given commands to check out all the branches:

git checkout feature1
git checkout feature2
Checkout the branches
Checkout the branches

Step 4: Now, all the branches will be shown. Type the below-given command again:

git branch
Checkout the Branches
Checkout the Branches

E. Merge All the Branches to the Git Hub “master” Repository

Step 1: Run the below-given command to check out the “master” repository:

git checkout master
Switch to the “master” branch
Switch to the “master” branch

Step 2: When we run the below-given command, it shows the “conflicts” message.

git merge feature2
Merge the “feature1” branch
Merge the “feature1” branch

Step 3: Run the below-given command to check out the “merge conflict” resolution tool:

git mergetool --tool=
Run the “git merge tool” command
Run the “git merge tool” command

Step 4: Press “enter” from the keyboard & the “vimdiff” tool will be opened.

vimdiff tool
vimdiff tool

Step 5: Press “ESC” from the keyboard & type the below-given command to save & quit from all the files one by one:

:wq!
Save and Quit
Save and Quit

Press “enter” from the keyboard to completely save & exit from the files after running the :wq! command every time.

Git Merge-Conflict Case Study
Save and Quit
Save and Quit
Save and Quit
Save and Quit from Files
Save and Quit from Files

Step 5: The files have been successfully edited & merge conflict has been resolved.

Merge Conflict Resolved
Merge Conflict Resolved

Step 6: Open the main.c file using the below-given command:

sudo nano main.c
Edit the “main.c” file
Edit the “main.c” file

Step 7: Remove the unwanted code from here:

Remove the “Unwanted” Code
Remove the “Unwanted” Code
Clean Code
Clean Code

Step 8: Now, run the below-given command to check the status of files:

git status
Check the File Status
Check the File Status

Step 9: Run the below-given command to add the “main.c” file:

git add .
Stage “main.c” file
Stage “main.c” file

Step 10: Run the below-given command to commit changes:

git commit –m "first commit"
Commit the “main.c” file
Commit the “main.c” file

Step 11: Now, we will merge the “feature2” branch into the “master” branch. Run the below-given command first:

git merge feature2
Merge the “feature2” branch
Merge the “feature2” branch

Step 12: Run the below-given command to open the “vimdiff” editor:

git mergetool --tool=vimdiff
vimdiff command
vimdiff command

Step 13: Press “ESC” from the keyboard & type the below-given command to save & quit from all the files one by one:

:wq!
Save and Quit from the files
Save and Quit from the files

Press “enter” from the keyboard to completely save & exit from the files after running the :wq! command every time.

Second time save and quit
Second time save and quit
Third Time Save and Quit
Third Time Save and Quit
Third Time Save and Quit
Third Time Save and Quit

Step 14: All the files will be successfully merged. It means the “feature2” branch code will be successfully merged into the “master” branch.

feature2 successfully merged
feature2 successfully merged

Step 15: Open the “main.c” file using the below-given command in the master:

sudo nano main. c
Edit the “main.c” file
Edit the “main.c” file

Step 16: Remove the unwanted lines from the “main.c” file.

Remove the unwanted lines
Remove the unwanted lines
Again, Remove the Unwanted Codes from Below part of the Code
Again, Remove the Unwanted Codes from Below part of the Code

Do “CTRL+X” to exit & press “Y” to save the file. Press “enter” from the keyboard to completely exit from the “main.c” file.

Again, Remove the Unwanted Codes from Below part of the Code
Save and Exit from the “main.c” file

Step 17: Run the below-given command to add the untracked changes:

git add .
Again, stage the main.c file
Again, stage the main.c file

Step 18: Save all the changes using the below-given command:

git commit –m "Second Commit"
Again, commit the main.c file
Again, commit the main.c file

Problem (3) Solution: Finally push all the branches to GitHub

Step 1: Type the below-given command to add the repository to “Git Hub Local”:

git remote add merge-conflict https://github.com/visaltyagi/merge-conflict.git
Repository Added in the Local Machine
Repository Added in the Local Machine

Step 2: Check repository is added or not, use the below-given command:

git remote –v

It will be successfully fetched in the repository.

Repository Successfully Fetched
Repository Successfully Fetched

Step 3: Run the below-given command to delete the “main.c.orig” file:

sudo rm -r main.c.orig
main.c.orig deleted
main.c.orig deleted

Step 4: Run the below-given command to push all the changes to the “Git Hub” master branch:

git push merge-conflict master

Put the username & password here.

We will put the username as our “Git Hub Account” username & password as “Git Hub Token”.

Push the “master” branch to the “GitHub” Account
Push the “master” branch to the “GitHub” Account

The “master” branch changes will be successfully pushed.

Step 5: All the changes will be successfully pushed to the “master” branch in “Git Hub Remote Repository”.

New main.c file
New main.c file

Step 6: Open the “main.c” file, all new changes will be shown.

Open the “main.c” file
Open the “main.c” file

Other Devops Case Studies:

Containerized an HTML Website using Docker on Production Environment — Docker Case Study

Creating an Architecture using Terraform on AWS — Terraform Case Study

Integration of DevOps Tools with Jenkins — Jenkins Case Study

Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study

How to Deploy a Sample Website on Kubernetes Using Ingress — Kubernetes Case Study

--

--