Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Published
4 min read
A

Hi, I’m Abdul Samad. A web development learner and tech enthusiast. I write about what I learn, share practical coding tips, and publish in-depth blogs on programming and modern web development.

Check out my full collection of blogs on Hashnode: https://abdulsamad30.hashnode.dev/

Connect with me on X for quick updates and insights: @abdul_sama60108

Introduction: What is Git?

If you are learning programming, sooner or later you will hear one word everywhere : Git.

Git is a version control system. In simple terms, Git helps you track changes in your code, save progress, and go back in time if something breaks.

Imagine working on a project for days and accidentally deleting an important file. Without Git, you are stuck. With Git, you can restore your project to a previous working state in seconds.

Git was created to solve real problems faced by developers:

  • Managing code changes

  • Working safely

  • Collaborating with others

In short: Git keeps your code safe and organized.


Why Git Is Used

Git is not optional in modern development. It is used because it solves real problems.

1. Track Code Changes

Git keeps a history of every change made to your project. You can see:

  • What changed

  • When it changed

  • Who changed it

2. Undo Mistakes

If a new change breaks your project, Git allows you to roll back to a previous version without panic.

3. Work Without Fear

You can experiment freely. If something fails, just go back to a stable commit.

4. Collaboration

Multiple developers can work on the same project without overwriting each other’s work.

5. Professional Requirement

Every serious company expects developers to understand Git basics.


Git Basics and Core Terminologies

Before learning commands, you must understand how Git work and its basic terms.

Repository (Repo)

A repository is a project folder that Git tracks.

Once a folder becomes a Git repository, Git starts monitoring changes inside it.


Working Directory

This is where you write and edit code.
Files here are not automatically saved in Git history.


Staging Area

The staging area is where you prepare changes before saving them permanently.

Think of it as:

“These are the exact changes I want Git to remember.”


Commit

A commit is a snapshot of your project at a specific moment.

Each commit has:

  • A unique ID

  • A message describing the change

Commits form the history of your project.


Branch

A branch allows you to work on features without touching the main code.

You can experiment safely and merge later.


HEAD

HEAD points to your current location in the project’s history.

Simply put:

HEAD tells Git where you are right now on which specific commit or branch.


How Git Internally Works

Git File Flow

Working Directory → Staging Area → Repository


Local Repository Structure Overview

A Git repository contains:

  • Your project files

  • A hidden .git folder that stores history and metadata


Common Git Commands (Beginner-Friendly)

git init

Initializes a new Git repository.

git init

Use this when:

  • Starting a new project

  • You want Git to track files


git status

Shows the current state of your project.

git status

It tells you:

  • Modified files

  • Staged files

  • Untracked files


git add

Adds files to the staging area.

git add file.txt

Or add everything:

git add .

This means:

“I want Git to include these changes in the next commit.”


git commit

Saves a snapshot of staged changes.

git commit -m "Initial commit"

A commit message should clearly explain what changed.


git log

Shows the commit history.

git log

You can see:

  • Commit IDs

  • Author

  • Date

  • Commit message


Basic Developer Workflow Using Git

This is how developers use Git daily.

Step 1: Create a Project

mkdir my-project
cd my-project

Step 2: Initialize Git

git init

Now your folder is a Git repository.


Step 3: Create a File

touch index.html

Step 4: Check Status

git status

Git shows the file as untracked.


Step 5: Stage the File

git add index.html

Now the file is in the staging area.


Step 6: Commit the File

git commit -m "Add initial HTML file"

Your project state is now saved.


Step 7: Make Changes and Commit Again

Edit the file, then repeat:

git add .
git commit -m "Update HTML structure"

Each commit adds a new point in history.


Commit History Flow

Commits form a timeline that you can move through.