Git for Beginners: A Simple Guide to Version Control
Every developer has heard the name Git, but many beginners feel confused about why Git is used and what version control actually means.
If questions like these come to your mind:
What is Git?
Why do developers use it?
What problem does version control solve?
Then this blog is for you.
By the end of this article, you will clearly understand what Git is, why it is important, its basic commands, and how developers use Git for version control in real projects.
So, let’s break the jargon step by step.
What is Git?
Git is a distributed version control system (VCS) that helps developers track changes in their code over time.
In simple terms, Git keeps a history of your code—what changed, when it changed, and who changed it.
An Easy Way to Understand Git
Think about how you usually work on code.
Sometimes, before experimenting with new changes, you:
Copy your project folder
Rename it like
project_backuporproject_oldMake changes in the original folder
You do this so that if something breaks, you can go back to the working version.
Most seniors still do this, but you should not. It is not a good practice.
Now, a question may come to your mind:
What is version control?
A version control system helps you save different versions of your work, so you can go back to an older version whenever you need.
Let’s understand this with an example.
Imagine you are working on a project and you save files like this:
project_v1project_v2project_finalproject_final_finalproject_final_last
This quickly becomes confusing, right?
git init
The git init command initializes your project folder with Git.
When you run this command, Git creates a hidden folder named .git inside your project.
You can see this folder by:
Opening your project in File Explorer
Enabling “Show hidden files”
Once enabled, you will see the .git folder.
This .git folder is very important because:
It stores all the history of your code
It tracks every change you make
It manages Git version control
Important:
If you delete the .git folder, all your Git history and backups will be lost, and your project will no longer be under version control.
Follow along with me.
Create an empty folder named
learn_git.Open this folder in VS Code.
Inside the folder, create a file named
index.js.Add the following code to the file:
console.log("learning git");
Now you have a simple project ready to be tracked using Git.

Now, we will initialize Git inside this folder and start version controlling our code.
Now open the terminal in VS Code by pressing:
Ctrl + ~
In the terminal, run the following command:
git init
After running git init Git will initialize your folder as a repository, and you will see a message similar to:
Initialized empty Git repository in /path/to/learn_git/.git/

This message confirms that Git has been successfully initialized in your project folder.
Your project is now ready to track changes using Git.
Now, let’s understand .gitignore.
Why Do We Use .gitignore?
Before tracking files with Git, create a .gitignore file in the root of your project.
Now, a question may come to your mind:
If we want Git to track files, then why do we need
.gitignore?
This is a very normal and important question.
Let’s understand why .gitignore exists.
Purpose of .gitignore
We use .gitignore to tell Git which files it should NOT track.
Sometimes, our application contains sensitive information that must not be exposed.
For example, when integrating a payment gateway like Razorpay, we use API keys or secret keys.
These keys:
Must be kept private
Should never be pushed to GitHub or any public repository
To solve this, we store such secrets in a file called .env (environment variables).
Since .env contains sensitive data, we do not want Git to track it.
That’s where .gitignore helps.
Practical Example
Now, follow these steps:
Step 1: Create a .env file
In your project root folder, create a file named .env and add a dummy key:
RAZORPAY_SECRET_ID=ierxedressl
This is just a dummy value for learning purposes.
Step 2: Create a .gitignore file
Create a file named .gitignore in the root folder and add this line:
.env

This tells Git to ignore the .env file.
Now, your secret file will not be tracked by Git.
Adding Files to the Staging Area
To start tracking files, we use the git add command.
Track a single file
git add filename
Track all files
git add .
This command adds files to the staging area.
What is the Staging Area?
The staging area is a place where you prepare your changes before saving them permanently.
You can think of it as:
A checkpoint before committing
A place to review or test changes
If something goes wrong, changes can be discarded before committing
Final Step
Now run the following command:
git add .

You will notice:
Normal files are staged
The
.envfile appears in gray color because it is ignored and not being tracked
That means .gitignore is working correctly!
git status
The git status command is used to check the current state of your files.
It shows:
Which files are in the staging area
Which files are modified but not staged
Which files are untracked
Which files are ignored
Run the command:
git status

This helps you understand what Git is tracking at the moment and what actions you need to take next.
Understanding Branches in Git
When you run git status, you will see something like:
On branch master

Now a question may come to your mind:
What is a branch, and why do we need it?
What is a Branch?
You can think of a branch as a separate path of development.
master(ormain) → the main and stable pathOther branches → temporary paths for new features or experiments
Final and stable changes always live on the master branch.
Why Do We Need Branches?
Let’s understand this with an example.
Imagine multiple developers are working on the same project:
One developer is working on a cart feature
Another is working on an order feature
If everyone works directly on the master branch, changes may conflict and break the code.
So instead, developers create separate branches like:
cart_featureorder_feature
Each branch tracks changes independently, without affecting the master branch.
git commit
What Does git commit Mean?
A commit is like creating a save point in a game.
Once you commit:
Your changes are safely saved
You can come back to this point anytime
Now commit your changes by running the following command in the terminal:
git commit -m "save master changes"
This command creates a save point for your current work on the master branch, allowing you to safely track and restore these changes later if needed.
Creating a New Branch
To create a new branch and switch to it, run:
git checkout -b dev_branch
This command does two things:
Creates a new branch named
dev_branchSwitches you to that branch
Now you are working on dev_branch, not on master.
Making Changes on a Branch
Open index.js and update it:
console.log("on dev_branch");

Now add the changes to the staging area:
git add .
Commit the changes:
git commit -m "changes done on dev_branch"
Switching Between Branches
To switch back to the master branch, run:
git switch master
The git switch command is used to move between branches.
Now open index.js again.
You will notice that the changes from dev_branch are not present.
That’s because branches are isolated from each other.
Merging Branches
To bring changes from dev_branch into master, use merge.
Run this command while on the master branch:
git merge dev_branch
After running this command, you may encounter a merge conflict.
A merge conflict happens when Git is unable to automatically combine changes from different branches because the same part of a file was modified in both branches.
When this occurs, Git will pause the merge and ask you to manually resolve the conflict before continuing.
Don’t worry—merge conflicts are common and are a normal part of working with Git, especially in team projects.

When a merge conflict occurs, VS Code will show a “Merge Editor” button at the top of the file.
Click on the “Resolve in Merge Editor” button.
The Merge Editor allows you to:
See both versions of the code
Choose which changes to keep
Or combine both changes easily

Now click on the right arrow icon in the left panel of the Merge Editor.
After doing this, the red error indication will disappear.
Next, click on the “Complete Merge” button.
Congratulations! You have successfully merged the code from dev_branch into the master branch. your branch.
Now, when you open index.js, you will see the merged changes applied to the file.
Pushing Code to GitHub (Remote Repository)
So far, all work is done locally.
Now let’s push the code to the cloud using GitHub.
Step 1: Create a GitHub Repository
Go to GitHub
Create an account (if you don’t have one)
Create a new repository named
learn_git
Enter name learn_git and then scroll down and click on create repository

Step 2: Add Remote Repository
Copy the repository URL (from the green Code button) and run:
git remote add origin <your-repository-url>
This command connects your local project to the GitHub repository.
Step 3: Push Code to GitHub
git push -u origin master
What Does -u origin master Mean?
origin→ name of the remote repositorymaster→ branch you are pushing-u→ sets the default upstream branch
After this, you can simply use:
git push
in the future without typing the branch name again.
Git Commands Index (Quick Reference)
git init→ initialize Gitgit status→ check file statusgit add→ add files to staging areagit commit→ save changesgit checkout -b→ create & switch branchgit switch→ switch branchesgit merge→ merge branchesgit remote add origin→ connect to GitHubgit push→ upload code to GitHub
Now you have learned:
Why branches are important
How developers work safely in teams
How to merge changes
How to push code to GitHub
These are the core Git commands every developer must know to work on real-world projects


