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

Visal Tyagi
DevOps-Guides
Published in
12 min readMay 22, 2024

Tasks to Be Performed:

1. Put master.txt on the master branch, stage, and commit

2. Create 3 branches: public 1, public 2 and private

3. Put public1.txt on public 1 branch, stage, and commit

4. Merge public 1 on the master branch

5. Merge public 2 on the master branch

6. Edit master.txt on a private branch, stage, and commit

7. Now update branch public 1 and public 2 with new master code in private

8. Also update the new master code on the master

9. Finally update all the code on the private branch

Before Checking this Assignment, kindly check these assignments:

Note: We have created an EC2 instance named as “Git”, you have to create an instance before performing this assignment. For creating the instance, you can check “Git-Assignment-1” Solution.

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

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

Git-Assignment-4 (Create Two Branches & Merge These Branches to the Master Branch)
Git-Assignment-4 (Create Two Branches & Merge These Branches to the Master Branch)

Note: We have created an EC2 instance named as “assignment4”, you can create an instance before performing this assignment.

Problem (1) Solution: Put master.txt on the master branch, stage, and commit

Step 1: First, create an “assignment4” directory using the below-given command:

mkdir assignment4

Run the below-given command & the “assignment4” directory will be shown here.

ls  
Create assignment 4 directory
Create assignment 4 directory

Step 2: Go to the “assignment4” directory using the below-given command:

cd assignment4
Go to assignment 4 directory
Go to assignment 4 directory

Step 3: Create a “master.txt” file using the below-given command:

touch master.txt

Run the below-given command & you will notice that “master.txt” has been created:

ls  
Create master.txt file
Create master.txt file

Step 4: Use the below-given command to initialize the “Git”:

git init
Initialize the Git Repository
Initialize the Git Repository

The “Git” remote will be successfully initialized.

Step 5: Now, add or stage the “master.txt” file in “Git” using the below-given command:

git add master.txt

Use the below-given command to check whether the “master.txt file” is added or not.

git status
Stage master.txt file
Stage master.txt file

Step 6: Now, we will commit the “master.txt” file in the “master” branch using the below-given command:

git commit –m "committing master.txt ".
Committing master.txt file
Committing master.txt file

The “master.txt” file will be successfully committed in the “master” branch.

Step 7: Use the below-given command to check out the content of the “master” branch:

git show-branch master
Show master branch content
Show master branch content

Step 8: Create a New Repository named as “Git-Assignment-4” in the “GitHub” account.

Click on “New”.

Create New Repository
Create New Repository

Step 9: Choose the following options here:

Repository Name: — Git-Assignment-4

Description: — Git Assignment 4

Choose the “Public” option to make this repository public.

Put Repository Name and Description
Put Repository Name and Description

Step 10: Enable the option “Add a README file” by choosing this option.

Enable the README option

Step 11: The “Git-Assignment-4” Repository will be successfully created.

Git-Assignment-4 Repository Created
Git-Assignment-4 Repository Created

Step 12: Now, go to the “Git” machine & use the below-given command to add “Repository URL”:

git remote add Git-Assignment-4 git@github.com:visaltyagi/Git-Assignment-4.git
Add Git Hub Remote Repository to Local Machine

The repository (Git-Assignment-4) will be successfully added.

Step 13: To check whether the GitHub Repository (Git-Assignment-4) will be added or not, use the below-given command:

git remote –v
Remote Repository Added to Local
Remote Repository Added to Local

It will show the repository URL here.

Step 14: Run the below-given command to check how many branches are present in the “Git-Assignment-3 repository”:

git branch
master branch shows
master branch shows

Step 15: Exit from “assignment4” using the below-given command:

cd ..
Exit from Directory
Exit from Directory

Step 16: Create the public key using the below-given command:

ssh-keygen
Create the public & private keys
Create the public & private keys

Press “enter” three times, and both public & private keys will be successfully added.

Step 17: Go to the “.ssh” directory using the below-given command:

cd .ssh

Run the below-given command & the “id_rsa.pub” will be shown:

ls
Go to the .ssh directory
Go to the .ssh directory

Step 18: Run the below-given command to view the public key content:

sudo cat id_rsa.pub
Copy the public key content
Copy the public key content

Copy this content of the public key from here.

Step 19: Go to the “GitHub” account. Go to “Settings” in “Profile”.

Go to Settings
Go to Settings

Step 20: Click on the “SSH and GPG Keys” section.

Go to SSH and GPG Keys
Go to SSH and GPG Keys

Step 21: Click on “New SSH Key” in the “SSH Keys” section.

Create New SSH Key
Create a New SSH Key

Step 22: Choose “Title” as “SSH Key”, while pasting the public key content in the “Key” section.

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCtplZNy+2HNhjzuH9ZYDhU+H4D+kCKvfmjzdWA7XTPIW/8iE5sCEZV9rLAWg7Xi//KXAilZwNYVXZ75OsadSXSYDCN5xngK3U+cXk3n9LbOXkCVrLeSllkKv7evL6QH7egKf06RqcuF/dJga22lnv8JmwmixJPg+V62CwpO3LSbXZfzya1oCpBTJ/EOXUcsmXtmvM+xPUBP9e1lo9EPODiFD2uLhZHMC2N7OhpEK3Mn9MtQsi2anzm2BDDetVY55Y4gb5bUHCQuYFS/tvsWsaf4q/9Id3UF43KBhOg+p2scWQ/tmc6fl+DrR8QZiIycImEigMEDIGDIkQ/fTonsbEXeJ9pFlYWiNO9FBwM3tVMcWKrcrdjqmS87zIHA1wI6hOlRFCSpYOz5Tenqd0QV/X77yHmSSuI0oRJlR/+bdC+Rju4MgwY1fRQa4MtYZvJNSUmVHU9yu/gD5sFXnv40zPOvXK89cMOPv/bHXi1XBa1qSRLSoMYKYYkDyIjDbgOHe0= ubuntu@ip-172–31–45–133

Add SSH Key
Add SSH Key

Click on “Add SSH Key”.

Step 23: Confirm the “SSH Key” by pasting the password. Click on “Confirm”.

Confirm Access
Confirm Access

Step 24: “SSH Key” will be successfully added.

SSH Key Added Successfully
SSH Key Added Successfully

Step 25: First, exit from the .ssh directory using the below-given command:

cd .ssh

Go to the “assignment4” directory using the below-given command:

cd assignment4

Go to the “Git” machine. Put this URL to push the branch in the “Git-Assignment-4” repository.

Run the below-given command:

git push Git-Assignment-4 master

Press “enter” from the keyboard & “master” repo will be successfully pushed to the “Git-Assignment-4” repository.

Push master branch to Remote Repository
Push master branch to Remote Repository

Step 26: Go to “Github Repository: Git-Assignment-4” & refresh the browser.

master branch pushed
master branch pushed

A message will be shown as “master has recent pushes (1 minute ago)”.

Step 27: Go to “master” & “master.txt” will be displayed here.

master.txt file
master.txt file

Step 28: In the “Github repository”, “main” is a default file, which can’t be deleted due to the “default” branch.

So, we will now make the “master” branch our “default” branch.

Click on “Settings”.

Go to Settings
Go to Settings

Step 29: Click on the “Arrow” sign.

Click Arrow Sign
Click Arrow Sign

Step 30: Choose “master” here & click on “update”.

Update master here
Update master here

Step 31: Click “I understand, update the default branch”.

Agree with terms
Agree with terms

Step 32: Now, “master” has been a “default” branch which can’t be deleted until “default” status.

master as a default branch
master as a default branch

Step 33: Click on “ode”.

Go to the code section
Go to the code section

Step 34: Click on “2 Branches”.

Click Hyperlink
Click Hyperlink

Step 35: Click on the “delete” option.

main branch
main branch
Delete the main branch
Delete the main branch

Step 36: The “main” branch will be successfully deleted & now “master” as a default branch will be shown.

master as a default branch
master as a default branch

Step 37: Now, only one branch will be shown here.

Git Hub Repository
Git Hub Repository

Problem (2) Solution: Create 3 branches: public 1, public 2 and private

Step 1: Now, we will create the three branches such as “public1”, “public2” & “private”.

Use the below-given command:

git branch public1
git branch public2
git branch private

Now, we will run the below-given command to determine how many branches are created.

git branch
Create the Branches
Create the Branches

All the branches (public1, public2, private) will be successfully created

Step 2: Push all the repository to Git Hub Account using the below-given command:

git push Git-Assignment-4 public1 public2 private
Push Branches to the Remote Repository
Push Branches to the Remote Repository

All branches will be successfully pushed to the “Git-Assignment-4” repository.

Step 3: Go to “Github Repository”, all branches will be successfully pushed.

All Branches Pushed
All Branches Pushed

Problem (3) Solution: Put public1.txt on public 1 branch, stage, and commit

Step 1: First, we will go inside the “public” branch using the below-given command:

git checkout public1
Switch to “public1” branch
Switch to “public1” branch

Step 2: Create a “public.txt” file using the below-given command:

touch public.txt

Run the below-given command & the “public.txt” will be successfully created:

ls  
Create the public.txt file
Create the public.txt file

Step 3: Stage the “public.txt” file using the below-given command:

git add public.txt
Stage the public.txt file
Stage the public.txt file

Step 4: Commit the “public.txt” file using the below-given command:

git commit -m "committing master.txt"
Committing the “master.txt” file
Committing the “master.txt” file

The file (public.txt) will be successfully committed.

Problem (4) Solution: Merge public 1 on the master branch

Step 1: Go to the “master” branch using the below-given command:

git checkout master

Step 2: Merge the “public1” branch with “master” using the below-given command:

Again, switch to the master branch
Again, switch to the master branch

Step 2: Merge the “public1” branch with “master” using the below-given command:

git merge public1
public1 branch successfully merged
public1 branch successfully merged

The “public1” branch will be successfully merged.

Problem (5) Solution: Merge public 2 on the master branch

Step 1: Use the below-given command to merge the “public2” branch with the “master” branch:

git merge public2
public2 merged with the master branch
public2 merged with the master branch

The “public2” will be successfully merged with the “master” branch.

Problem (6) Solution: Edit master.txt on a private branch, stage, and commit

Step 1: Go to “private” using the below-given command:

git checkout private
Switch to the private branch
Switch to the private branch

Step 2: Use the below-given command to edit the file:

nano master.txt
Edit the “master.txt” file
Edit the “master.txt” file

Step 3: Write some content here such as: “New Edits in master.txt file”.

master.txt file content
master.txt file content

Step 4: Run the below-given command to check the “master.txt” status:

git status
master.txt status
master.txt status

Step 5: Stage the “master.txt” file using the below-given command:

Staged the “master.txt” file
Staged the “master.txt” file

The file will be successfully staged.

Step 6: Run the below-given command to commit the “master.txt” file using this command:

git commit –m "commit master.txt"
Commit the “master.txt” file
Commit the “master.txt” file

Step 7: Push All the changes to the “Git-Assignment-4” repository using the below-given command:

git push -u -f Git-Assignment-4
Push “master.txt” to Git Hub private branch
Push “master.txt” to the Git Hub private branch

All changes have been successfully pushed to the private branch in the “Git-Assignment-4” repository.

master.txt pushed to private
master.txt pushed to private

Here, the new “master.txt” code of the “private” branch will be updated in the “public1” & “public2” branches.

Problem (7) Solution: Now update branch public 1 and public 2 with new master code in private

Step 1: Go to the “public1” branch using the below-given command:

git checkout public1
Switch to the public1 branch
Switch to the public1 branch

Step 2: Run the below-given command to merge the “private” branch with “public1”:

git merge private
Merge private branch with public1 branch
Merge private branch with public1 branch

Step 3: A message will be shown like the below image.

Save and Exit from the file
Save and Exit from the file

Do “CTRL+X” to exit from the file.

Step 4: The “private” branch will be successfully merged with the “public1” branch.

The private branch successfully merged with public1
The private branch successfully merged with public1

Step 5: Run the below-given command to update the local repository using this command:

git push –u Git-Assignment-4
push public1 to remote repository
push public1 to the remote repository
master.txt in public1
master.txt in public1

The code will be successfully updated in “master.txt” in “public1” in the “Git-Assignment-4” repository.

Step 6: Again, go to the “public2” branch using the below-given command:

git checkout public2
Switch to the public2 branch
Switch to the public2 branch

You will successfully land inside the “public2” directory.

Step 7: Merge the “private” branch to “public2” using the below-given command:

git merge private
private branch merged successfully with public2
private branch merged successfully with public2

The “private” branch will be successfully merged with the “public2” branch.

Step 8 Run the below-given command to update the local repository using this command:

git push –u Git-Assignment-4
push public2 branch to the remote repository
push the public2 branch to the remote repository
Updated master.txt file in public2
Updated master.txt file in public2

The code will be successfully updated in “master.txt” in “public2” in the “Git-Assignment-4” repository.

Problem (8) Solution: Also update the new master code on the master

Step 1: Go to the “master” branch using the below-given command:

git checkout master
Checkout the master branch
Checkout the master branch

Step 2: Run the below-given command to merge the “private” branch with “master”:

git merge private
merge private branch to master
merge private branch to master

Step 3: A message will be shown like the below image.

Save and exit from the file
Save and exit from the file

Do “CTRL+X” to exit from the file.

Step 4: The “private” branch will be successfully merged with the “master” branch.

private merged with master successfully
private merged with master successfully

Step 5: Run the below-given command to update the local repository using the below-given command:

git push –u Git-Assignment-4
Push master to GitHub Account
Push master to GitHub Account
master branch with updated file
master branch with updated file

The code will be successfully updated in “master.txt” in the “master” branch in the “Git-Assignment-4” repository.

Problem (9) Solution: Finally update all the code on the private branch

Step 1: Go to the “private” branch using the below-given command:

git checkout private
Switch to the private branch
Switch to the private branch

Step 2: Merge “public1” with the “private” branch using the below-given command:

git merge public1
Git-Assignment-4 (Create Two Branches & Merge These Branches to the Master Branch)
Merge the public1 branch with the private

Step 3: Run the below-given command to change into the “Git-Assignment-4” repository on “Git Hub”.

Push private to the Remote Repository
Push private to the Remote Repository

Step 4: Now, all the codes have been successfully updated to the “private” branch.

master.txt file
master.txt file

The “public.txt” has no content to show here.

public.txt file
public.txt file

Check these Git Case Assignment & Studies Also:

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

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

--

--