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

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

Tasks to Be Performed:

1. Create a Git working directory with feature1.txt and feature2.txt in the master branch

2. Create 3 branches develop, feature1 and feature2

3. In the develop branch, create develop.txt, do not stage or commit it

4. Stash this file and check out the feature1 branch

5. Create a new.txt file in the feature1 branch, stage, and commit this file

6. Checkout to develop, unstash this file and commit

7. Please submit all the Git commands used to do the above steps

Git-Assignment-2
Git-Assignment-2

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

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.

Problem (1) Solution: Create a Git working directory with feature1.txt and feature2.txt in the master branch

Step 1: First, we will create a directory using the below-given command:

mkdir assignment2
Create a Directory
Create a Directory

Do “ls” here & both the directories are shown here.

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

cd assignment2  
Go to “assignment2” directory
Go to “assignment2” directory

Step 3: Create “feature1.txt” & “feature2.txt” file using the below-given command:

touch feature1.txt feature2.txt
Create feature1.txt & feature2.txt files
Create feature1.txt & feature2.txt files

Do “ls” here & you will notice that both “feature1.txt” & “feature2.txt” have been created.

Step 4: Now, initialize the Git using the below-given command:

git init  
Initialize the Git Repository
Initialize the Git Repository

Git will be successfully initialized.

Step 5: Now, add or stage both the files in “Git” using the below-given command:

git add feature1.txt feature2.txt

Use the below-given command to check whether both files are added or not.

git status
Stage Both the Files
Stage Both the Files

Step 6: Now, we will commit both the files in the “master” branch using the below-given command:

git commit –m "committing both feature1.txt & feature2.txt"
Commit feature1.txt & feature2.txt files
Commit feature1.txt & feature2.txt files

Both files 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
Checkout Master Branch Content
Checkout Master Branch Content

Step 8: Create a New Repository named “Git-Assignment-2” 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-2

Description: — Git Assignment 2

Repository Name & Description
Repository Name & Description

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

Enable the README File Option
Enable the README File Option

Step 11: Repository (Git-Assignment-2) will be successfully created.

Repository (Git-Assignment-2) Created
Repository (Git-Assignment-2) Created

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

git remote add Git-Assignment-2 https://github.com/visaltyagi/Git-Assignment-2.git

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

Repository Added to Local Environment
Repository Added to Local Environment

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

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

It will show the repository URL here.

Step 14: Run this command to check how many branches are present in the “Git hub repository”.

git branch
Checkout the Branch
Checkout the Branch

Step 15: Go to “Git Hub Profile” & click on “Settings” here to create the access token. The access token will be used when you push the branch to the “Git Hub Repository”.

Go to Settings
Go to Settings

Step 16: Click on “Developer Settings” at the end.

Go to Developer Settings
Go to Developer Settings

Step 17: Click on “Personal Access tokens > Tokens (classic)”.

Go to Token (Classic)
Go to Token (Classic)

Step 18: Click on “Generate new token > Generate new token (classic)”.

Generate a New Token
Generate a New Token

Step 19: Choose “Note” as “Access Token”. While, in “Select scopes”, choose the “repo” option.

Choose Token Configuration Here
Choose Token Configuration Here

Step 20: Click on “Generate token”.

Click on Generate Token
Click on Generate Token

Step 21: The token will be successfully generated. Please copy & paste this token into a separate notepad & save it. Otherwise, every time you have to generate a new token.

Copy the token by clicking on the icon.

Copy the Token
Copy the Token

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

Command: git push Git-Assignment-2 master

Username: visaltyagi

Password: Personal Access Token here

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

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

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

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

Pushed Message Shown in GitHub
\Pushed Message Shown in GitHub

Step 24: Go to “master” & both “feature1.txt” & “feature2.txt” will be shown here.

Master Pushed Successfully with Files
Master Pushed Successfully with Files

Step 25: 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”.

Go to Settings
Go to Settings

Step 26: Click on the “Arrow” sign.

Click on Arrow
Click on Arrow

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

Update master branch here as default
Update master branch here as default

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

Agree with Terms
Agree with Terms

Step 29: 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 30: Click on “code”.

Go to the code section
Go to the code section

Step 31: Click on “2 Branches”.

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

Step 32: Click on the “delete” option.

main branch
main branch
Delete main branch
Delete the main branch

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

only master branch shown
Only the master branch shown

Problem (2) Solution: Create 3 branches develop, feature1 and feature2

Step 1: Now, we will create the three branches such as “develop”, “feature1” & “feature2”.

Use the below-given commands:

git branch develop
git branch feature1
git branch feature2

Now, we will run the below-given command to check out how many branches are created:

git branch
Branches Created Successfully
Branches Created Successfully

All the branches will be successfully created

Problem (3) Solution: In the develop branch create develop.txt, do not stage or commit it

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

git checkout develop
Switch to “develop” branch
Switch to “develop”

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

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

touch develop.txt

Check the file creation, use the below-given command to check the file status:

git status

It will ask to add this file but here we only have to create a “develop.txt” file, not staged or committed.

Create develop.txt file
Create develop.txt file

Problem (4) Solution: Stash this file and check out to the feature1 branch

“Stash” means hiding the file. We have to stash the “develop.txt” file & move it to the “feature1” branch.

Step 1: Use the below-given command to hide or stash the file:

git stash -u
Stash develop.txt file
Stash develop.txt file

Do “ls” here & the “develop.txt” has been successfully stashed.

Step 2: Go to the “feature1” branch using the below-given command:

git checkout feature1
Checkout to feature1 branch
Checkout to feature1 branch

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

Problem (5) Solution: Create a new.txt file in the feature1 branch, stage, and commit this file

Step 1: Use the below-given command to create the “new.txt” file:

touch new.txt
Create new.txt file
Create new.txt file

Step 2: To check out the file in the “feature1” branch, run the below-given command:

git status
Check File Status in feature1 branch
Check File Status in the feature1 branch

The “new.txt” file has been successfully created.

Step 3: Use the below-given command to stage or add the file:

git add new.txt
Add “new.txt” file
Add “new.txt” file

Step 4: Commit this file using the below-given command:

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

The new.txt file will be successfully committed.

Problem (6) Solution: Checkout to develop, unstash this file, and commit

Step 1: Checkout to the “develop” branch using the below-given command:

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

Step 2: Run the below-given command & the “develop.txt” file will not be shown here because it is hidden.

ls
Check files here
Check files here

Step 3: Use the below-given command to unstash the file:

git stash pop
Unstashed develop.txt file
Unstashed develop.txt file

The “develop.txt” has been successfully unstashed.

Step 4: Stage the “develop.txt” file using the below-given command:

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

The “develop.txt” has been successfully staged.

Step 5: Run the below-given command to check whether “develop.txt” has been staged or not:

git status
Check the “develop.txt” file Status
Check the “develop.txt” File Status

Step 6: Commit the “develop.txt” file using the below-given command:

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

The “develop.txt” file has been successfully committed.

Step 7: Now, we will push all the branches to the repository (Git-Assignment-2) using the below-given command:

git push –u Git-Assignment-2 develop feature1 feature2

Put your username (Github username) & password (access token).

Press “enter” from the keyboard after entering the username & password.

Push the Branched to GitHub Repository
Push the Branched to GitHub Repository

Step 8: All branches will be successfully moved to “GitHub Repository”.

Branches Successfully Pushed to GitHub Repository
Branches Successfully Pushed to GitHub Repository

Step 9: With these branches, the committed file will be shown.

Files Shown Under “develop” Branch
Files Shown Under “develop” Branch

Problem (7) Solution: Please submit all the Git commands used to do the above steps

Step 1: Run this command to check the command used for this assignment: history

mkdir assignment2
ls
cd assignment2
touch feature1.txt feature2.txt
ls
git init
git add feature1.txt feature2.txt
ls
git status
git commit -m "commiting both feature1.txt & feature2.txt"
git branch
git checkout master
ls
git remote add Git-Assignment-2 https://github.com/visaltyagi/Git-Assignment-2.git
git remote -v
git branch
git branch -d develop feature1 feature2
ls
git branch
git push Git-Assignment-2 master
git branch develop
git branch feature1
git branch feature2
git checkout branch
git branch
git checkout develop
touch develop.txt
ls
git status
git stash -u
ls
git checkout feature1
git checkout develop
git checkout feature
git checkout feature1
touch new.txt
ls
git status
git add new.txt
git commit -m "committing new.txt file"
git status
git checkout develop
ls
git stash pop develop.txt
git stash pop
ls
git add develop.txt
git status
git commit -m "commiting the develop.txt file"
git status
git checkout master
git branch
git push -u Git-Assignment-2 develop feature1 feature2
git checkout feature1
ls
git checkout master
history

Checkout this Git Hub Assignments Also:

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

Check these Git Case Studies Also:

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

--

--