Skip to main content

Command Palette

Search for a command to run...

🔍Git for Beginners: Basics and Essential Commands

Published
7 min readView as Markdown

✍️Introduction:

When I started learning programming, I used to save my project files like this:

project_final.zip
project_final_v2.zip
project_final_really_final.zip

Very soon, I realized this was messy and confusing.
That’s when I learned why developers use Git.

In this blog, I’ll explain what Git is, why it is used, and how beginners can start using Git step by step.

✍️What is Git?

In simple language git means version control system, track changes and helps developer to work safely.

But more technical way: Git is a distributed version control system to track changes in source code during development.

Now you think what is version control system?? It’s nothing but in simple way: It keeps a history of your work and allows you to go back to any previous version whenever needed.

Every developer use Git as a complete copy of the entire project on their own system.

✍️Why Git is used?

Git is used because it solves many real-world problems:

1. Losing Code by Mistake ❌

Problem:
You delete or overwrite a working file and can’t recover it.

How Git Helps:
Git keeps a complete history of every change.
You can easily go back to a previous working version.

2. Multiple People Editing the Same File 🧑‍🤝‍🧑

Problem:
Two developers change the same file and one person’s work gets lost.

How Git Helps:
Git merges changes safely and shows conflicts if any.

👉 Real life: Google Docs but for code

3. Confusing File Versions 📁

Problem:
Files like:

final.cpp
final_v2.cpp
final_latest.cpp
final_latest_fixed.cpp

How Git Helps:
Git stores versions internally using commits with messages.

👉 Real life*:* Clean history instead of messy file names

4. Experimenting Without Fear 🧪

Problem:
You’re scared to try a new feature because it may break the code.

How Git Helps:
You can create a branch, test freely, and discard it if it fails.

👉 Real life: Like testing ideas in a separate notebook

5. Tracking Who Changed What 🕵️

Problem:
A bug appears and no one knows who caused it.

How Git Helps:
Git shows:

  • Who changed the code

  • When it was changed

  • Why (commit message)

👉 Real life: CCTV for code changes

6. Fixing Bugs Faster 🐞

Problem:
You don’t know which change introduced a bug.

How Git Helps:
You can:

  • Compare versions

  • Go back to a stable commit

  • Identify the faulty change

👉 Real life: Time-travel to find mistakes

7. Working Offline 🌐

Problem:
No internet but you still want to code and save progress.

How Git Helps:
Git works offline and syncs later when online.

👉 Real life: Notes app that syncs when internet is back

💡Because of these reasons, Git is used in almost every software company today.

✍️Git basics and Core Terminologies:

Before learning Git commands or terms, it’s important to understand how Git works behind the scenes.

Git works in three main stages:

  1. Working Directory

  2. Staging Area

  3. Repository

📌 Diagram: Git Basic Workflow

Working Directory → Staging Area → Repository

🔹Repository (Repo)

A repository is a folder where Git tracks all changes of your project.

It contains:

  • Your project files

  • Complete history of changes

  • Information about commits and branches

You create a repository using:

git init

📘 Real-life example:
A repository is like a Google Drive folder that remembers every edit you’ve ever made.


Working Directory

The working directory is where you actually write and modify your code.

Whenever you:

  • Create a file

  • Edit code in VS Code

  • Delete or update a file

You are working inside the working directory.

📝 These changes are not saved by Git automatically until you tell Git to save them.

Staging Area

The staging area is a temporary place where you select which changes should be saved.

You use the staging area to tell Git:

“These are the changes I want to include in my next save.”

Command used:

git add filename

or to add everything:

git add .

📘 Real-life example:
Like selecting photos before uploading them to Instagram.

🔹Commit

A commit is a permanent snapshot of your project at a specific point in time.

Each commit includes:

  • A unique ID

  • A message describing the change

  • Date and time

Command:

git commit -m "Added login feature"

📘 Important:
Once a commit is created, you can always go back to it.

🔹Branch

A branch allows you to work on new features or experiments without affecting the main code.

By default, Git creates a branch called main.

📌 Diagram: Branching in Git

main
  |
  |── feature-login
  |
  |── bug-fix

Another view:

A ── B ── C (main)
          \
           D ── E (feature branch)

📘 Real-life example:
Like writing rough work on a separate notebook page while keeping the main notes clean.


🔹HEAD

HEAD is a pointer that shows:

Where you are currently working in Git

If you switch branches, HEAD moves with you.

📘 Simply put:
HEAD = your current position in the project.

😀Now come to all common Git commands:

🟢 git init — Initialize a Repository

Initializes a new Git repository in the current project directory.

git init

Purpose:

  • Starts Git tracking for a project

  • Creates a .git directory

Use this command once when starting a new project.

🟢 git status — Check Repository Status

Displays the current state of the working directory and staging area.

git status

Shows:

  • Modified files

  • Staged files

  • Untracked files

This command is useful to understand what changes are pending.

🟢 git add — Stage Changes

Moves changes from the working directory to the staging area.

Add a specific file:

git add filename

Add all changes:

git add .

Purpose:

  • Prepares changes for commit

🟢 git commit — Save Changes

Records staged changes permanently in the repository.

git commit -m "Commit message"

Purpose:

  • Creates a snapshot of the project

  • Stores changes with a descriptive message

Commit messages should clearly describe the change.

🟢 git log — View Commit History

Displays the commit history of the repository.

git log

Shows:

  • Commit hash

  • Author

  • Date and time

  • Commit message

Useful for tracking changes over time.

🟢 git diff — View File Differences

Shows differences between modified files and the last committed version.

git diff

Purpose:

  • Review changes before staging or committing

🟢 git branch — Manage Branches

Lists, creates, or deletes branches.

List branches:

git branch

Create a new branch:

git branch branch-name

Purpose:

  • Enables parallel development

🟢 git checkout — Switch Branches

Switches between branches or commits.

git checkout branch-name

Purpose:

  • Move the HEAD pointer to another branch

🟢 git merge — Merge Branches

Combines changes from one branch into the current branch.

git merge branch-name

Purpose:

  • Integrates completed work into another branch

🟢 git clone — Copy a Repository

Creates a local copy of a remote repository.

git clone repository-url

Purpose:

  • Download an existing project to your system

🟢 git pull — Fetch and Merge Changes

Fetches updates from a remote repository and merges them into the current branch.

git pull

Purpose:

  • Keep the local repository up to date

🟢 git push — Upload Local Changes

Uploads local commits to a remote repository.

git push

Purpose:

  • Share changes with others

🟢 git remote — Manage Remote Repositories

Displays or manages remote repository connections.

git remote -v

Purpose:

  • View linked remote URLs

    😀😀Final Thoughts:

    Git may look complicated at first, but it becomes much simpler once you understand the purpose of each command. Focus on learning the basics well instead of trying to remember everything at once.

    Mistakes are part of the learning process, and Git is designed to help you recover from them. With consistent practice, these commands will soon feel natural.

    Up next, we will explore a complete Git workflow that beginners can follow in real projects.