Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Published
4 min read

What Is the Problem That Is Resolved by Git?

Before Git existed, managing code was messy, risky, and inefficient—especially when more than one developer was involved.

Imagine this situation 👇

You are working on a project and you save your files like this:

Now ask yourself:

  • Which file is the latest?

  • What exactly changed between versions?

  • What if a new change breaks the app?

  • How do you safely collaborate with another developer?

This is the core problem Git solves.


What Is Git?

Git is a Distributed Version Control System (DVCS) that helps developers track changes in their code, collaborate efficiently, and maintain a complete history of their project.

Instead of saving multiple copies of files, Git stores snapshots of your project and allows you to move between them with ease.

Why “Distributed”?

Every developer has a full copy of the project and its history on their own machine.
This means:

  • You can work offline

  • No single point of failure

  • Faster operations


Git Basics and Core Terminologies

Let’s understand the most important Git concepts.

Repository (Repo)

A repository is a project tracked by Git.

It contains:

  • Your source code

  • All commits (history)

  • Branches and metadata

Created using:

git init

Staging Area (Index)

A temporary area where you select changes before saving them permanently.

Think of it as:

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


Commit

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

Each commit has:

  • A unique ID (hash)

  • Author

  • Date

  • Message describing the change


Commit

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

Each commit has:

  • A unique ID (hash)

  • Author

  • Date

  • Message describing the change


Branch

A branch is a parallel version of your code.

  • main (or master) → default branch

  • Feature branches → for new features or fixes


HEAD

HEAD points to:

  • Your current branch

  • The latest commit you’re working on


Git Architecture (Conceptual Diagram)

Working Directory → Staging Area → Repository

Visual Flow

Edit files
   ↓
git add
   ↓
Staging Area
   ↓
git commit
   ↓
Git Repository

Common Git Commands (With Examples)

1. Initialize a Repository

git init

Creates a .git folder and starts tracking your project.


2. Check Status

git status

Shows:

  • Modified files

  • Staged files

  • Untracked files


3. Stage Files

git add filename.js

Stage everything:

git add .

4. Commit Changes

git commit -m "Any message associated with particular commit"

Good commit messages are short and descriptive.


5. View Commit History

git log

Short version:

git log --oneline

6. Create a Branch

git branch feature-branch

Switch to it:

git checkout feature-branch

Or in one command:

git checkout -b feature-branch

7. Merge a Branch

git checkout main
git merge feature-branch

A Basic Git Workflow (From Scratch)

Let’s see how a developer typically uses Git.

Step 1: Create a Project

mkdir my-app
cd my-app
git init

Step 2: Create a File

touch index.js

Add code:

console.log("Hello Git");

Step 3: Check Status

git status

Step 4: Stage the File

git add index.js

Step 5: Commit the Changes

git commit -m "Initial commit"

Step 6: Make More Changes

Edit index.js again, then repeat:

git add .
git commit -m "Update greeting message"

Commit History Flow Diagram

* c3f9a2 (HEAD -> main) Update greeting message
* a8d12b Initial commit

Each commit builds on top of the previous one.


Best Practices for Beginners

  • Commit small and logical changes

  • Write clear commit messages

  • Use branches for features and fixes

  • Run git status often

  • Don’t fear Git — you can always undo mistakes