Create & Delete the Branches in Git Hub from Local & Remote Repository — Git Assignment 3
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
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
Do “ls” here & all the three directories will be shown here.
Step 2: Go to the “assignment3” directory using the command:
cd assignment3
Step 3: Create a “main.txt” file using the command:
touch main.txt
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
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
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 "
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
Step 8: Create a New Repository named “Git-Assignment-3” in the “GitHub” account.
Click on “New”.
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.
Step 10: Enable the option “Add a README file” by choosing this option.
Step 11: The “Git-Assignment-3” Repository will be successfully 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
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
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
Step 15: Create the public key using the command:
ssh-keygen
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 ..
Step 17: Go to the “.ssh” directory using the command:
cd .ssh
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 this content of the public key from here.
Step 19: Go to the “GitHub” account. Go to “Settings” in “Profile”.
Step 20: Click on the “SSH and GPG Keys” section.
Step 21: Click on “New SSH Key” in the “SSH Keys” section.
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
Click on “Add SSH Key”.
Step 23: Confirm the “SSH Key” by pasting the password. Click on “Confirm”.
Step 24: The “SSH Key” will be successfully added.
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.
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)”.
Step 27: Go to the “master” & “main.txt” will be displayed here.
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”.
Step 29: Click on the “Arrow” sign.
Step 30: Choose “master” here & click on “update”.
Step 31: 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.
Step 33: Click on “Code”.
Step 34: Click on “2 Branches”.
Step 35: Click on the “delete” option.
Step 36: The “main” branch will be successfully deleted & now “master” as a default branch will be shown.
Step 37: Now, only one branch will be 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 (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
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
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
Step 4: Use this command to commit the “develop.txt” file:
git commit –m "committing develop.txt file"
Step 5: Switch to the “F1” branch using the below-given command:
git checkout F1
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
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
Step 8: Use the below-given command to commit the “f1.txt” file:
git commit –m "committing f1.txt file"
Step 9: Switch to the “F2” branch using the below-given command:
git checkout F2
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
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
Step 12: Use the below-given command to commit the “f2.txt” file:
git commit –"committing 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
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.
Step 3: Refresh the repository (Git-Assignment-3) & all the branches will be shown successfully.
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
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
The “F2” branch will be successfully deleted.
Step 3: Run the below-given command to check out how many branches are available:
git branch
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
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.
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