Sitemap
Aditya Kumar

Aditya Kumar

BTech | Security Software Engineer & Researcher | Cloud Security | Python, AWS, MERN, ReactJS | Mail: adii.utsav@gmail.com

Git & GitHub Step-by-Step: Part 2

15 min readFeb 28, 2026
Press enter or click to view image in full size

Can we un-stage a file which is already in staging stage?

Yes, we can do so.

Delete the folder of mypanda. Create the git repo for git tuts. Using: git init

Check status of the repo using:

git status

Then use git add . to add files in repo.
Then use git commit -m “Initial commit” to commit.

Press enter or click to view image in full size

Check status of the new repo using:

git status

Now make some changes in the first_renamed.txt file. And save it, then again check status of the git tuts repo.

Press enter or click to view image in full size

Now, to add first_renamed.txt to staging area we will use:

git add first_renamed.txt

Press enter or click to view image in full size

Now, what to do to un stage that file?

git restore — -staged first_renamed.txt

Press enter or click to view image in full size

Suppose by mistake we have modified something which is not very required. So we want that file to go again in that previous commit state. Can we do so?

Yes, we can do so.

First make the change in the file first_renamed.txt, then check git status.

Press enter or click to view image in full size

Now, to go into the previous commit state, we will use following command:

git checkout — first_renamed.txt

Press enter or click to view image in full size
Press enter or click to view image in full size

Also, to undo the changes for many files, or say completely getting previous commit stage of the folder (git tuts), we can use the following command: (Before it make changes to any number of files present in that folder)

git checkout -f

Press enter or click to view image in full size

Working with remote repositories:

Creating a repository online:

Go to the website www.github.com then click on the “+” icon and then choose “New Repository” , write name of the repository as “GiTutorialdemo” and write some description about it. Choose whether to make it available public or privately. Then click on create.

Press enter or click to view image in full size

New window appear as:

Press enter or click to view image in full size

We know,

Push: it means to throw the code in the repo

Pull: it means to fetch the code form the repo

So, now in last ss we can see some instructions as:

Press enter or click to view image in full size

So, we will use the first row in case we don’t have any existing repo in our PC. But, since we already have the repo, we will use the commands shown in 2nd row.

To start adding the git repo (created online) we will us the command, where origin is used to indicate the name of the git repo.

git remote add origin https://github.com/Rememberful/GitTutorialdemo.git

Now, to check it we will use the command:

git remote

Press enter or click to view image in full size

To get the link of the link to fetch and push we will use the command:

git remote -v

Press enter or click to view image in full size

Now, how to push the files in this repo?

To do so we need to get the authentication key, or say SSH key. For that go to he settings, then create on “SSH key” and write name of it.

Press enter or click to view image in full size

Since, we don’t have any SSH key, we will generate one. Copy & paste the command in git bash:

ssh-keygen -t rsa -b 4096 -C adii.utsav@gmail.com

The above code is copied from the website:

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Then,

Press enter or click to view image in full size

Now, copy the key using:

Clip < ~/.ssh/id_ed25519.pub

Press enter or click to view image in full size

Copy and paste the key to git hub account:

Press enter or click to view image in full size

Now, to push the files in our repo we will use the following command:

git push -u origin main

For case (master) is written beside the file path of the repo, replace ‘main’ with ‘master’.

Press enter or click to view image in full size

This will push all the files which we have asked to be committed.

Press enter or click to view image in full size

Now, suppose we created one more file named forgithub.txt and wrote something in it, then we can also push it to the online git hub, using the command as follows: (before it create the text file)

Press enter or click to view image in full size

Commands:

git status

git add .

git commit -m “Added forgithub file to github”

git push -u origin main

Press enter or click to view image in full size

This will push that new text file created by us to the repo online:

Press enter or click to view image in full size

Setting Alias in Git:

Alias means to use the user specified commands to a certain task, such as we can use git st instead of git status for the same work if we manage to change the meaning of git st.

Let’s first run git st without doing anything, it will give an error.

Press enter or click to view image in full size

So, how to make it work?

Use the command:

git config –global alias.st status

Press enter or click to view image in full size

Then check if it works or not:

git st

Press enter or click to view image in full size

Similarly, we can change the commit command to ci as shown below:

Press enter or click to view image in full size

Can we write something like this to un stage the file? Yes, we can.

git config –global alias.unstage ‘restore — -staged — –’

Press enter or click to view image in full size

Similarly, we can make an alias to see the what someone committed in last commit:

git config –global alias.last ‘log -p 1’

Press enter or click to view image in full size

Can we merge work of two people in git?

Yes, we can.

A and B created a website, and later, C joined the team and was asked to make changes to the website. A and B suggested that C create a branch to continue his work, which would prevent the website from being disrupted and allow them to compare C’s work with the existing work done by A and B. Git enables C’s work to be merged with the work completed before C joined, along with the work that A and B have continued since then.

So, what’s the master branch?

The master branch is the most important branch, allowing the creation of different branches without disturbing the main branch.

Let’s see example:

Suppose in our current repo, git tuts, we want to make some changes, but we wish that it shouldn’t disturb the main/master branch. Then what we can do?

To develop a new branch and switch to it:

git checkout -b develop

Press enter or click to view image in full size

Now, suppose we made some changes where we renamed `first_renamed.txt` to `renamed.txt`, then checked the git status. After that, we used `git add .` followed by `git status` to check the staged state, and then committed the changes.

Press enter or click to view image in full size

Now, commands:

Press enter or click to view image in full size

Now, suppose due to some reason we want the repo to be in exact same state as it was earlier. For that we will use the command:

git checkout main

Press enter or click to view image in full size

It will automatically bring us to the main state of the repo. Where the first_renamed.txt is not renamed.txt.

Press enter or click to view image in full size

How to see the branch name of the repo?

git branch

Press enter or click to view image in full size

Branches are not committed by default to the git hub.

Working on a project using the git, git hub, VS code:

Install VS code and create a folder named Mywebsite inside the git repo, git tuts, since remove the git tuts as git repo using the command rm-rf .git

Press enter or click to view image in full size

Now, open VS Code and create a file named `index.html` in the MyWebsite folder. Install the extension called Live Server, then save the file and run it using the “Go Live” button.

Next, manually open the MyWebsite folder, right-click while holding the Shift key, and select the option for Git Bash. Check the status; since it’s opened for the first time, it will show that it’s not a Git repository. To make it a repository, use the following command to initialize the repo and commit the files inside it.

Press enter or click to view image in full size

Now, suppose we want to create a branch named ‘tryCleanup’ to make some changes in it. Command:

git checkout -b tryCleanup

Press enter or click to view image in full size

Now, suppose we made some changes in the index.html, then we will save it and go to the live server again. But, suppose now we want to undo those changes.

Press enter or click to view image in full size

Then? Can we undo it?

git checkout main

Press enter or click to view image in full size

We will observe that the changes we made in the index.html are no longer there, we are back again to the last commit or say main branch.

So, can we again go to the tryCleanup branch? (Since we had exited from there to the main branch recently)

Yes, we can do so. Using the command:

Get Aditya Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

git checkout tryCleanup

Press enter or click to view image in full size

We will again back in the tryCleanup branch and we will find that the change we had done in that branch are still present there.

Don’t forget to commit before you change the branch!!!!!!!!

Now, suppose we made some changes in the tryCleanup branch, now we want to skip the staging area and merge the tryCleanup branch to the main branch.

Press enter or click to view image in full size

So, we made changes, skipped the staging area, and switched to the `main` branch. Now, to merge the `main` branch with the `tryCleanup` branch, use the following command:

git merge tryCleanup

It’s possible that the changes made in `tryCleanup` may conflict with the code in the `main` branch, but VS Code allows us to choose between the original version and the incoming changes. However, in our case, we didn’t encounter any conflicts.

Press enter or click to view image in full size

Now, check the git status:

Press enter or click to view image in full size

If there is something at the unmerged path, use the following commands to stage and commit the changes:

git add .

git commit

However, since the `index.html` file in both branches doesn’t conflict, we haven’t encountered anything in the unmerged path.

What is the merge conflict?

A merge conflict in Git occurs when Git is unable to automatically merge changes between two branches. This usually happens when two branches have made changes to the same part of a file or when one branch has made changes to a file that the other branch has deleted.

When a merge conflict arises, Git marks the conflicting parts in the file and pauses the merge process until you manually resolve the conflict. You need to decide which changes to keep (or create a combination of both) before completing the merge.

For example, if we edit a file in different branches and attempt to merge them, and if the same portion of the file has been edited in both branches, Git will notify us of a conflict and will not allow the merge. At this point, we will have the option to choose how to resolve the conflicting changes. These changes are marked by conflict resolution markers, such as `<<<<<`.

How to manage merge conflict? We will see later!

What is branch management?

Branch management in Git refers to the practice of organizing, creating, maintaining, and managing different branches in a repository to streamline development processes, collaboration, and version control. Proper branch management helps developers work on multiple features, bug fixes, or experiments simultaneously without disrupting the main codebase. This ensures that changes can be isolated, reviewed, tested, and merged in an organized way.

Git allows us to create as many branches as we want without taking up much space because it uses pointers to keep track of the changes. Instead of duplicating files, Git stores changes incrementally and uses these lightweight pointers to reference commits, making branching highly efficient.

Let’s see the example: First let’s create git repo in the Git tuts folder and commit as:

Press enter or click to view image in full size

Now, in VS code, under git tuts folder, make a file name index.html and save it.

Press enter or click to view image in full size

Then check status:

Press enter or click to view image in full size

Now track it and commit:

Press enter or click to view image in full size

Now, suppose we made some changes in index.html as:

Press enter or click to view image in full size

Then again checked the status:

Press enter or click to view image in full size

Now, add it and commit:

Press enter or click to view image in full size

Similarly, we edited the index.html for 2nd time, and added as well as committed the changes:

Press enter or click to view image in full size

Now, let’s create a branch named ‘issue1’ and make some changes in the index.html and commit it without keeping it in the staging process as:

Press enter or click to view image in full size

Similarly, done for c5, c7 with some changes in index.html for c5 and c7.

Press enter or click to view image in full size

Now, switch to ‘main’ branch:

Press enter or click to view image in full size

Made changes in index.html and committed:

Press enter or click to view image in full size

Now, we try to merge the issue1 branch and main branch, merge conflict will be observed:

Press enter or click to view image in full size

In VS code this issue will be pointed as:

Press enter or click to view image in full size

We have few options; we can select what we want or manually delete the unwanted things. Suppose we selected the ‘Accept Incoming change’ option.

Press enter or click to view image in full size

Now, when we use see the status of the repo we will see that it is written ‘unmerged path’:

Press enter or click to view image in full size

To manage it we will add the index.html as:

git add index.html

Press enter or click to view image in full size

Now, let’s merge the issue1 with main:

Press enter or click to view image in full size

We can see and verify it as well using:

git log

Press enter or click to view image in full size

How to work with the branch management?

To see the name of the existing branches:

git branch

Press enter or click to view image in full size

Suppose we want to create one more branch:

Press enter or click to view image in full size

Then made some changes in the index.html, and then added and committed:

Press enter or click to view image in full size

Now, if we want to go back to the main branch, we can do as:

git checkout main

Press enter or click to view image in full size

To get the has value of the commit as well as the comment of the commit:

git branch -v

Press enter or click to view image in full size

To see the merged branches:

git branch –-merged

Press enter or click to view image in full size

To see the non-merged branches:

git branch –-no-merged

Press enter or click to view image in full size

To delete the branch:

git branch -d issue2

But, since issue2 is not merged anywhere, it will give a warning/hint to be sure for it.

Press enter or click to view image in full size

Since, issue1 is merged so we can delete it:

Press enter or click to view image in full size

Branching workflow:

Primarily there be two types of branches in a project:

1. Long running branches: Exists till project exists

2. Topic branches: Works until the topic get completed

Press enter or click to view image in full size

Pushing Git Branches To Remote Repositories:

Let’s first create a new branch in git tuts, as:

Press enter or click to view image in full size

Now, delete a file from the folder, say we deleted forgithub.txt. Then check status, followed by adding and then commit.

Press enter or click to view image in full size

Now, change the branch to main:

Press enter or click to view image in full size

Now, check remote using:

git remote

Press enter or click to view image in full size

Since, it didn’t shown any output it means that, there is no remote over there, now open github.com and create one new repo.

Press enter or click to view image in full size

Now, copy the link which appears as:

Press enter or click to view image in full size

Now, check name of the remote using:

git remote

Press enter or click to view image in full size

Then,

Press enter or click to view image in full size

Now, to push use the command:

git push -u origin main

Press enter or click to view image in full size

In the demorepo at github.com we can find:

Press enter or click to view image in full size

Now, check status:

Press enter or click to view image in full size

Now, in the remote repo, we can see that the branches are not available as sections, so we will separately push them. Now, to push the ‘bugfix’ branch we will first get inside it, using:

git checkout bugfix

Now, to push bugfix as a branch in the remote repo, we will use the command:

git push origin bugfix

Press enter or click to view image in full size

See, on the remote rep, we will be able to see that there are now two branches:

Press enter or click to view image in full size

In order to push the bugfix as mybugfix we will use:

git push origin bugfix:mybugfix

Press enter or click to view image in full size
Press enter or click to view image in full size

Now, to delete the bugfix from the remote repo we can use:

git push -d origin bugfix

Press enter or click to view image in full size

Contact Me:
📧 Email: adii.utsav@gmail.com
🔗 LinkedIn: https://www.linkedin.com/in/aditya-kumar-3241b6286/
💻 GitHub: https://github.com/Rememberful

--

--

Aditya Kumar
Aditya Kumar

Written by Aditya Kumar

BTech | Security Software Engineer & Researcher | Cloud Security | Python, AWS, MERN, ReactJS | Mail: adii.utsav@gmail.com