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

Visal Tyagi
DevOps-Guides
Published in
10 min readMay 20, 2024

Tasks to Be Performed:

1. In the master branch, commit the main.txt file

2. Create a Git working directory, with the following branches:

● Develop

● F1

● f2

3. Put develop.txt in develop branch, f1.txt and f2.txt in f1 and f2 respectively

4. Push all these branches to GitHub

5. On local delete the f2 branch

6. Delete the same branch on GitHub as well

Checkout these Assignments Solutions before going to Git-Assignment-3 Solution:

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

Git Assignment 3
Git Assignment 3

To check the repository or copy the Git Commands, check the “Git-Assignment-3” Repository:

Problem (1) Solution: In the master branch, commit the main.txt file

Step 1: First, we will create a separate directory using this command:

mkdir assignment3
Create assignment 3 directory
Create assignment 3 directory

Do “ls” here & all the three directories will be shown here.

Step 2: Go to the “assignment3” directory using the command:

cd assignment3
Go to the “assignment3” directory
Go to the “assignment3” directory

Step 3: Create a “main.txt” file using the command:

touch main.txt
Create a main.txt file
Create a main.txt file

Do “ls” here & you will notice that “main.txt” has been created.

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 “main.txt” file in “Git” using the below-given command:

git add main.txt

Use the below-given command to check whether the “main.txt file” has been added

git status
Stage the “main.txt” file
Stage the “main.txt” file

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

git commit –m "committing main.txt "
main.txt committed
main.txt committed

The “main.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
Showing the master branch content
Showing the master branch content

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

Click on “New”.

Create a New Repository
Create a New Repository

Step 9: Choose the following options here:

Repository Name: — Git-Assignment-3

Description: — Git Assignment 3

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

Choose Repository Name and Description
Choose the Repository Name and Description

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

Choose the README file Option & Create a Repository
Choose the README file Option & Create a Repository

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

Git-Assignment-3 Repository Created
Git-Assignment-3 Repository Created

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

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

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

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

git remote –v
Check that Repository Added or Not
Check that the Repository Added or Not

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
Show the Branches
Show the Branches

Step 15: Create the public key using the 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 16: Exit from the “assignment3” directory using the below-given command:

cd ..
Exit from assignment3 directory
Exit from the assignment3 directory

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

cd .ssh
Go to the .ssh directory
Go to the .ssh directory

Do “ls” here & “id_rsa.pub” will be shown.

Step 18: Run the below-given command to view & copy 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 AAAAB3NzaC1yc2EAAAADAQABAAABgQChTxB23QxsM+wfsaunKNltrWBlaCT57zmYf+pFWlx6K0nhBvh8vt2FMtlNcu8YauqBHJm6Zp8FBOiYl2kPEJR0pBmrOefrKOF6momHWk3C7wBI4+QYk04GOEhNg1k54e7weiQVRiGZ6ltQ5hqoJHF/ubE4MrwYf0AfOYD4CADNTozJSK6YvWYak05AKu5BW0yCdjR/5GaTPqEPD2EbYTCrY4UCio6vMU3go1KZegfY8ZL3+k1Jk3Fgu+OVnD8BlKkYIuMCjzIOFX1C7QYTJujXKejRQCiuJ+bo9hDkqcna+6n4uzd9vEhjSU4fehuwyWpPKuhFlKzSVyDGzW1zCG7HD4AykOHl1+c3Un9fvLtArjQJg2SCq6SxVejDFZfGoBaMBJXE5pFJPtRdbhgQaSeHQECLRJ9InlUFLuV0Qnlbht+TfwfbMxnqM84rkhTJ5mYgqIhBZ88g7wnYWld26Fy/jatsMDaSiVIoWqZV38erL+uMxQDasBowM1kfgSwxV+s= ubuntu@ip-172–31–45–133

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQChTxB23QxsM+wfsaunKNltrWBlaCT57zmYf+pFWlx6K0nhBvh8vt2FMtlNcu8YauqBHJm6Zp8FBOiYl2kPEJR0pBmrOefrKOF6momHWk3C7wBI4+QYk04GOEhNg1k54e7weiQVRiGZ6ltQ5hqoJHF/ubE4MrwYf0AfOYD4CADNTozJSK6YvWYak05AKu5BW0yCdjR/5GaTPqEPD2EbYTCrY4UCio6vMU3go1KZegfY8ZL3+k1Jk3Fgu+OVnD8BlKkYIuMCjzIOFX1C7QYTJujXKejRQCiuJ+bo9hDkqcna+6n4uzd9vEhjSU4fehuwyWpPKuhFlKzSVyDGzW1zCG7HD4AykOHl1+c3Un9fvLtArjQJg2SCq6SxVejDFZfGoBaMBJXE5pFJPtRdbhgQaSeHQECLRJ9InlUFLuV0Qnlbht+TfwfbMxnqM84rkhTJ5mYgqIhBZ88g7wnYWld26Fy/jatsMDaSiVIoWqZV38erL+uMxQDasBowM1kfgSwxV+s= ubuntu@ip-172–31–45–133
Add New SSH Key
Add New 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: The “SSH Key” will be successfully added.

SSH Key Successfully Added to Git Hub
SSH Key Successfully Added to Git Hub

Step 25: Go to the “Git” machine. Put this URL to push the branch in the “Git-Assignment-3” repository.

Run the below-given command:

git push Git-Assignment-3 master

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

Push the master branch to the remote repository
Push the master branch to the remote repository

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

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

master branch successfully pushed
master branch successfully pushed

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

The main.txt file
The main.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 as our “default” branch.

Click on “Settings”.

Make the “master” branch as default
Make the “master” branch as default

Step 29: Click on the “Arrow” sign.

Click on Arrow
Click on Arrow

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

Update “master” as default
Update “master” as default

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

Click “I understand, update the default branch”
Click “I understand, update the default branch”

Step 32: Now, the “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 “Code”.

Go to the “code” section
\Go to the “code” section

Step 34: Click on “2 Branches”.

Click on “2 Branches”
Click on “2 Branches”

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 shown by default
master as shown by default

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

Only master branch shown here
Only the master branch is shown here

Problem (2) Solution: Create a Git working directory, with the following branches:

● Develop

● F1

● f2

Step 1: Now, we will create the three branches such as “Develop”, “F1” & “F2”.

Use the below-given commands:

git branch Develop
git branch F1
git branch F2

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

git branch
All the Branches Created
All the Branches Created

All the branches (Develop, F1, F2) will be successfully created.

In starting, we have created a separate directory “assignment3” to perform this assignment.

Problem (3) Solution: Put develop.txt in the develop branch, f1.txt, and f2.txt in f1 and f2 respectively

Step 1: Switch to the “Develop” branch using the below-given command:

git checkout Develop
Go to the “develop” branch
Go to the “develop” branch

You will be successfully switched to the “Develop” branch.

Step 2: Create a “develop.txt” file using the command:

touch develop.txt

Check the file creation using the below-given command:

git status
Go to the “develop” branch & check the status
Go to the “develop” branch & check the status

It will ask to add this file. Now, we will perform the stage & commit to the “develop.txt” file in the “Develop” branch.

Step 3: Use the below-given command to stage the “develop.txt” file:

git add develop.txt
Stage the “develop.txt” file
Stage the “develop.txt” file

Step 4: Use this command to commit the “develop.txt” file:

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

Step 5: Switch to the “F1” branch using the below-given command:

git checkout F1
Switch to “F1” Branch
Switch to the “F1” Branch

You will be successfully switched to the “F1” branch.

Step 6: Create an “f1.txt” file using the below-given command:

touch f1.txt

Check the file creation using the below-given command:

git status
Create & Check f1.txt file Status
Create & Check f1.txt File Status

It will ask to add this file. Now, we will perform the stage & commit to the “f1.txt” file in the “F1” branch.

Step 7: Use the below-given command to stage the “f1.txt” file:

git add f1.txt
Create & Check f1.txt file Status
Stage the “f1.txt” file

Step 8: Use the below-given command to commit the “f1.txt” file:

git commit –m "committing f1.txt file"
Committing the “f1.txt” file
Committing the “f1.txt” file

Step 9: Switch to the “F2” branch using the below-given command:

git checkout F2
Switch to the “F2” Branch
Switch to the “F2” Branch

You will be successfully switched to the “F2” branch.

Step 10: Create an “f2.txt” file using the below-given command:

touch f2.txt

Check the file creation using the below-given command:

git status
Create the f2.txt file
Create the f2.txt file

It will ask to add this file. Now, we will perform the stage & commit to the “f2.txt” file in the “F2” branch.

Step 11: Use the below-given command to stage the “f2.txt” file:

git add f2.txt
Stage the “f2.txt” file
Stage the “f2.txt” file

Step 12: Use the below-given command to commit the “f2.txt” file:

git commit –"committing f2.txt file"
Committing the f2.txt file
Committing the f2.txt file

Problem (4) Solution: Push all these branches to GitHub

Now, we will push all the branches to the repository (Git-Assignment-3)

Step 1: Now, use the below-given command to push the branches in “GitHub Repository”:

git push Git-Assignment-3 Develop F1 F2
Push the Branches
Push the Branches

All the branches will be successfully pushed to “Git Hub repository (Git-Assignment-3).

Step 2: Go to “Github account” & also, you will notice the message that “Develop”, “F1” & “F2” have recent pushes available.

All Branched Pushed Successfully
All Branched Pushed Successfully

Step 3: Refresh the repository (Git-Assignment-3) & all the branches will be shown successfully.

Branches Shown in Remote Repository
Branches Shown in Remote Repository

Problem (5) Solution: On local delete the f2 branch

Step 1: First, go to the master branch using the below-given command:

git checkout master
Switch to the master branch
Switch to the master branch

You will be successfully moved to the “master” branch.

Step 2: Now, we will delete the “F2” branch from the remote using the below-given command:

git branch –D F2
Delete the F2 Branch from Local Repository
Delete the F2 Branch from the Local Repository

The “F2” branch will be successfully deleted.

Step 3: Run the below-given command to check out how many branches are available:

  git branch
All branches are shown here
All branches are shown here

The “F2” branch will not be shown here now.

Problem (6) Solution: Delete the same branch on GitHub as well

Step 1: Now, we will delete the branch from the “local” using the command:

git push Git-Assignment-3 --delete F2
Delete the F2 Branch
Delete the F2 Branch

The “F2” branch will be successfully deleted from the repository also.

Step 2: Go to “GitHub Repository” & refresh the browser. Click on “master” & “F2” branches will not be shown here.

Git Assignment 3
Delete the F2 from the Remote Repository

Check these Git Assignments & Case Studies Also:

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

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

--

--