Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
6 min read
Git for Beginners: Basics and Essential Commands
A

I’m learning web development and sharing what I understand to help other beginners.

Introduction

Git is a very important tool used by developers to manage their code.
Git is a tool that helps developers save their work and track changes.
In simple words, Git works like a smart save system for code and files.
Whenever we make changes in a project, Git remembers what was changed and when.
If something goes wrong, Git allows us to go back to an older version easily.

What is Git?

Git is a version control system.

In simple words:

Git helps you save your work, track changes, and go back to older versions when needed.

Real-life example:

Imagine you are writing notes in a notebook:

  • Day 1: You write some notes

  • Day 2: You add more notes

  • Day 3: You make changes

If you want to see what you wrote earlier, you can check old pages.

Git works in the same way, but for code and project files.

Git as a Distributed Version Control System

Git is called a distributed version control system.

Git is a distributed version control system, which means every developer has a full copy of the project on their own computer.
They do not depend on only one system to save their work.
Even if there is no internet, developers can still work and save changes locally.
This makes Git fast, safe, and reliable.

What does this mean?

  • Every developer has a full copy of the project

  • The project is stored on their own computer

  • Work can be done even without internet

Diagram: Distributed Git System

Developer A   Developer B   Developer C
     |             |             |
     |-------------|-------------|
            Git Repository

Each developer works independently but stays connected through Git.

Why Git is Used

Before Git, developers faced many problems.

Problems without Git:

  • Code can be lost

  • Files can be overwritten

  • No record of changes

  • Team work becomes confusing

Benefits of Git:

  • Code is saved safely

  • All changes are tracked

  • Easy teamwork

  • Can return to old versions anytime

Git is used because developers often make mistakes or need to change code many times.
Without Git, it is easy to lose work or overwrite files.
With Git, all changes are saved safely and can be tracked.
This makes work more organized and helps developers work confidently, even in a team.

That is why Git is used in almost every software company.

Git Basics and Core Terminologies

Below are the most important Git terms explained in a simple way.

Repository (Repo)

A repository is a place where your project is stored.

  • It contains all project files

  • It also contains the history of changes

Simple example:
A repository is like a folder that keeps everything related to your project.

A repository, also called a repo, is a folder that contains the project files and the history of changes.
When we start using Git in a folder, that folder becomes a repository.
Git stores all saved versions of the project inside this repository.
In simple terms, a repository is the main place where the project lives.

Diagram: Repository Structure

Project Folder (Repository)
│── index.html
│── style.css
│── script.js
└── .git (Git history)

Commit

A commit means saving your changes in Git.

  • Each commit has a message

  • The message explains what was changed

Simple example:
A commit is like clicking Save after completing some work.

A commit means saving the current work in Git.
Each time we complete a small task, we create a commit.
A commit includes the changes and a short message describing what was done.
It is similar to clicking the save button, but Git also remembers the history.

Branch

A branch is a separate version of your project.

  • Used to try new changes

  • Main project stays safe

Simple example:
It is like making a copy of your notebook and writing on it.

A branch is a separate copy of the project where we can work without affecting the main code.
Developers use branches to try new features or fix bugs safely.
The main branch remains stable while changes are made in another branch.
Once everything works fine, the changes can be added back to the main branch.

Diagram: Branching

Main Branch ──────●─────●─────●
                   \
                    ●─────●  (New Branch)

HEAD

HEAD shows your current working position in Git.

  • It points to the latest commit

  • It shows where you are working

Simple example:
HEAD is like a bookmark in a book.

HEAD shows the current position in the project.
It tells Git which commit or branch we are working on right now.
Whenever we make a new commit, HEAD moves to the latest one.
In simple words, HEAD means “you are here.”

Common Git Commands

These are the most commonly used Git commands for beginners.

git init

git init

Purpose: Start Git in a project

Explanation:
This command tells Git to start tracking your folder.

The git init command is used to start Git in a folder.
After running this command, Git begins tracking the project.
It is the first step when creating a new Git repository.

git status

git status

Purpose: Check file status

Explanation:
Shows which files are new, changed, or ready to save.

The git status command shows the current state of the project.
It tells us which files are new, changed, or ready to be saved.
This command helps developers understand what is happening in the project.

git add

git add .

Purpose: Prepare files for saving

Explanation:
Moves files to the staging area before saving.

The git add command is used to prepare files for saving.
It moves the changed files to a ready state.
Git will only save files that are added using this command.

git commit

git commit -m "Your message"

Purpose: Save changes

Explanation:
Creates a saved version of your work with a message.

The git commit command is used to save changes permanently.
Each commit has a message that explains what was changed.
This helps in understanding the project history later.

git log

git log

Purpose: View history

Explanation:
Shows all commits made in the project.

The git log command shows the history of commits.
It displays all saved versions of the project with messages and time.
This command is useful to review past work.

Basic Git Workflow (From Start to Save)

This is the basic workflow used by developers.

Step-by-step Workflow Diagram

Create Folder
     ↓
 git init
     ↓
 Edit Files
     ↓
 git status
     ↓
 git add .
     ↓
 git commit

Explanation:

  1. Create a project folder

  2. Start Git using git init

  3. Create or edit files

  4. Check changes using git status

  5. Add files using git add

  6. Save changes using git commit

The basic Git workflow starts by creating a project folder and running git init.
After making changes to files, we check the status using git status.
Then we prepare files using git add and save them using git commit.
This process is repeated whenever new changes are made.

Summary

  • Git helps manage and save code safely

  • It tracks changes and history

  • It supports teamwork

  • Git is essential for developers

With practice, Git becomes easy and powerful.