Skip to main content

Command Palette

Search for a command to run...

version Control (Git)

Updated
10 min read
S

Skilled in managing carrier-grade ISP infrastructure, enterprise environments, and server operations. Enthusiastic about optimizing high-performance networks and exploring emerging technologies. Committed to continuous learning and driven to leverage cloud solutions and automation tools to enhance innovation and efficiency.

Prerequisites Before Getting Started:

  • Ensure Git is installed.

  • Install VS Code.

  • Generate SSH keys, whether using Windows PowerShell or a Linux-based environment.

  • Utilize Google search, YouTube tutorials, and AI to quickly set up these prerequisites.

When we create a project or application we create different versions of it with multiple updates now consider every new update as a seperate folder with that update say you create a quiz application we may have different versions of with updated feature and so on e,g Quiz App 1 , Quiz App 2 and Final Quiz App
say in your Final quiz App there is a bug and now you want to return to the previous stable version Imagine this power this is called Version Control.

Categories of Version Control System

  1. Local Version System - Everything Thing is on the local system which is a drawback.

  2. Centralized Version Control System - Get a copy of code from the centralized system make the change and save it back on the system drawback (if the centralized system fails no more code

  3. Distributed version Control System(GIT)
    Entire commit history on local machine as well as a centralized system plus the ability to collaborate with other Users.

Next we set up the identity on the git using following commands

What is Git Identity?

Git identity consists of a user name (user.name) and email (user.email), which Git associates with commits to track authorship and maintain version control transparency.

Why is Git Identity Required?

  • Identifies who made changes.

  • Enables collaboration and accountability.

  • Required for Git services (GitHub, GitLab).

  • Ensures a consistent commit history.


PS C:\Users\Sumeet.Tanpure> git --global user.name "kashimo0054"
PS C:\Users\Sumeet.Tanpure> git --global user.email "myemail@gm"

Let's start by understanding the fundamentals. In this example, we'll use the GIT folder as our working directory. Additionally, we'll cover the staging environment and its role in version control.

In Git, the staging environment (also called the index or staging area) is an intermediate space where changes are prepared before committing them to the repository.

How It Works:

  1. Modified (Working Directory) – You edit files in your working directory.

  2. Staged (Index/Staging Area) – You add specific changes to the staging area using git add.

  3. Committed (Local Repository) – You finalize and save the staged changes using git commit.

Why Use the Staging Area?

  • Allows you to review and organize changes before committing.

  • Enables partial commits (staging only selected changes in a file).

  • Helps prevent accidental commits of unfinished work.

Next we start by creating local git repository how do we do this we first initialize the folder

PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git init

Next we check on git status to check on if our directory has been intialized or not ?

What Does git status Do?

The git status command shows the current state of your working directory and staging area. It helps you track changes before committing.

PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git status

As we can see, we are currently on the main branch with no commits. Next, let's create a sample file and move it to the staging environment using the git add command.


git add .\firstfile.txt

Our file is now in the staging environment, but since there are no commits yet, it hasn't been added to the local repository. Let's commit it to the local repository.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git commit -m "Myfirst commit"

From the image above, we can see that the file has been added to the local repository. We can use git log to view the commit history.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git log

we can additionally skip the staging environment for this case I have modified the file and can move file directly to the local repository


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git commit -a -m "MYsecondcommit"

Additionally we can also view the difference in modified file in both working directory and staging environment using the following commands. we have modified the file with random text for reference


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git diff

for viewing difference at working directory

PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git diff --staged

for viewing difference at staged environment


Next, let's learn how to delete files in Git.

What if we accidentally add a file to Git and need to remove it? For example, suppose we added credentials.txt to the local repository, but it contains sensitive data. We must ensure it's deleted before pushing it to the remote repository.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git rm --cached .\credential.txt

as soon as we deleted the file we get a untracked notification on the file by vscode notifying us of the recently deleted file.

Next, let's ensure connectivity to the remote repository. This allows us to share code with other developers and maintain a backup in case of local system failure.

GitHub, one of the most popular platforms, is widely used due to its open-source nature and large user base. Connectivity to a remote repository can be established in two ways:

  1. SSH – A secure method that eliminates the need to enter credentials repeatedly.

  2. HTTPS – Requires authentication each time you push changes to the repository.


Make sure to upload your SSH keys to your GitHub profile. Once done, create a new repository and follow the steps below.

Additionally, GitHub provides guidance on setting up connectivity from your local repository to the remote repository, as shown in the image below, after creating a new repository.

Since our remote repository didn’t have a README file, I created one, added it to the staging area, committed it, and pushed it to the remote repository for demonstration purposes as shown in the image below.


next let`s learn What is Git Tag?

We might have noticed many of the software tools have a tagging number this is useful to let users know incase there is a new version to the software also as developers we can have some meaningful changes to our code and update it with tags

Types of Git Tags

  1. Lightweight Tag – A simple reference to a commit (like a branch but fixed).
git tag v1.0

2) Annotated Tag – Stores metadata like the author, date, and message.


git tag -a v1.0 -m "Version 1.0 release"

In our example I have used annotated Tag below is the command for same


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git tag -a v1.1 -m "1st tag"

We have added our first tag with the descriptive message "1st tag." Additionally, we can use the git tag command to view the list of tags and git show to see detailed information about a specific tag.

PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git tag
v1.1

we can also push the tags to the remote repository using the command.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git push origin v1.1

Next let`s learn What is a Git Branch?

A Git branch is a separate line of development within a repository, allowing you to work on features or fixes without affecting the main codebase. It helps in parallel development and version control.

In collaborative development, multiple developers can work on different features in separate branches without affecting each other’s work. For example:

  • One developer works on payment gateway integration in a payment-gateway branch.

  • Another improves frontend interaction in a frontend-update branch.

This ensures isolated development and smooth integration when merging changes. Now, let’s dive into learning the related Git commands!


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git switch -c "feature1branch"
Switched to a new branch 'feature1branch'

We have created a new branch and observed that we have switched to it, as indicated by the * symbol pointing to the newly created branch, as shown in the image below.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git switch main

We can switch between branches using the git switch command and also delete a branch with the following command.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git branch -d feature1branch2

To create a difference between the main branch and feature1branch, I added a new file and updated some text in the README file of feature1branch, then staged the changes.


We can also compare the differences between branches using the following command. In this case, we checked the differences between the main and feature-1 branches.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder>  git diff main

We can push the newly created branch to the remote repository using the following command. Note that we specify the name of the newly created branch, not the main branch.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder>  git push origin feature1branch

now if we finally decide to merge the two branches after completion we can do that with the following command.

PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git merge feature1branch

Next, let’s explore how to revert changes in Git, whether they are staged or already committed.

Case 1: Staged Environment

If you've modified a file and added it to the staging area but now need to revert it, use the following command to reset it for a single file.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git reset <filename>

and if you've made changes to multiple files, you can use the following command.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git reset

Case 2: Committed but Not Pushed

If you've committed a file or folder but haven't pushed it to the remote repository yet, you can use the following command to undo the commit.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git reset HEAD~1

The above command is helpful for reverting a single commit. However, if you need to undo multiple commits, use the following command.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git reset <-commit hash->

Likewise, if you need to perform a hard reset and remove all files created or modified after a specific commit, use the following command.


PS C:\Users\Sumeet.Tanpure\Desktop\Git Folder> git reset --hard <-commit hash->

This will ensure that all changes are removed from both your working directory and the staged environment.

Next, let's learn how to contribute to an open-source project. If you believe your contributions involve significant changes, you can do so using Fork.

What is Fork?

In Git, a fork is a personal copy of a repository that exists independently of the original repository. When you fork a repository, you create a duplicate of it under your own GitHub (or GitLab, Bitbucket, etc.) account. This allows you to freely experiment with changes without affecting the original project.

Think of it as a rough copy of the project you want to work on. With that in mind, let's get started!

In the image above, I'm forking a random project for our use case. Next, click on the Fork option in the top-right corner. Once you do this, you'll be prompted to create a new repository.

Next, as a use case, I update the README file with random text and commit the changes.

Next, I commit the changes to the master branch. After that, you'll notice that our master branch is ahead of the original branch.

once we click on it we can compare the changes.

We can also create a pull request to merge our master branch with the original user's base repository. However, since our contribution wasn’t significant, we’ll skip this step as it was just for demonstration purposes.

Key Takeaways from This:

  • git init – Initializes a new Git repository in the current directory.

  • git status – Displays the current state of the working directory and staging area.

  • git add – Adds changes from the working directory to the staging area.

  • git commit – Saves staged changes as a new commit in the repository.

  • git log – Shows the commit history of the repository.

  • git clone – Creates a copy of an existing repository.

  • git tag – Marks a specific commit with a meaningful label.

  • git show – Displays details about a specific commit or object.

  • git switch -c – Creates and switches to a new branch.

  • git switch – Moves between existing branches.

  • git remote add – Links a local repository to a remote repository.

  • git push origin – Uploads local changes to a remote repository.

  • git pull – Fetches and merges changes from a remote repository into the local branch.

With that, I conclude my Git blog. Thank you, and happy learning! :)

More from this blog

Sumeet Tanpure

30 posts

As a network/system engineer with over half a decade of experience, I'm constantly driven to stay ahead of the curve in new technologies.