Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
4 min read
Git for Beginners: Basics and Essential Commands

Git is the tool that keeps track of how a project changes over time.

It helps developers save progress understand past changes and work on code without fear of losing it.


What Is Git?

Git is a version control system that tracks changes in code.

In simple terms Git maintains a history of a project. Every change can be saved reviewed or undone later. Instead of copying folders or creating manual backups Git provides a structured way to manage how code evolves.

Git works locally on a developer’s machine. An internet connection is not required to start using Git. Everything related to the project’s history is stored inside a hidden .git folder.


Why Git Is Used

Without Git developers rely on sharing ZIP files using multiple file names and creating manual backups.

As projects grow files change frequently. Bugs appear features stop working and it becomes difficult to identify when a problem was introduced.

Git removes this problem. It records what changed and when making it easier to find bugs and safer to experiment with code.


Git Basics and Core Terminologies

Repository (Repo)

A repository is a project that Git is tracking.

When developers run git init, Git creates a .git folder inside the project. This folder stores the complete history of changes made to that project.


Commit

A commit records how the project looks at a specific point in its history.

Each commit represents a moment where developers decided that the current state of the code is worth saving. Commits include a unique ID a message and a position in the project’s history.


Branch

A branch is a separate line of development.

Branches allow developers to work on features or experiments without affecting the main codebase. This makes parallel development possible and keeps changes isolated until they are ready.


HEAD

HEAD points to the current position in the project’s history.

In simple terms HEAD tells Git which commit is currently checked out.


File States in Git

Files tracked by Git move through different states.

  • U - Untracked (Red)
    Git sees the file but it is not tracked yet.

  • M - Modified (Red)
    The file is tracked but it has changed after the last commit.

  • A - Added (Staged) (Green)
    The file is added to the staging area and ready to be committed.

  • D - Deleted (Red)
    A tracked file has been removed.

  • R - Renamed (Green)
    A file was renamed and Git keeps its history.

  • C - Copied (Green)
    A file was copied from another tracked file.

  • U - Unmerged (Red)
    A merge conflict exists and needs manual resolution.

  • Clean (White)
    No pending changes. Everything is committed.


File State Flow


Git Workflow (How Git Actually Works)

Workflow Diagram


Essential Git Commands (With Examples)

git init

Initializes a new Git repository.


git init

This command is usually run once when starting a project.


git status

Shows the current state of files.


git status

It tells developers:

  • Which files are untracked

  • Which files are modified

  • What is staged for commit


git add <fileName>

Adds files to the staging area.


git add index.js
git add .

This tells Git which changes should be included in the next commit.


git commit -m "message"

Creates a commit.


git commit -m "add initial files"

A good commit message explains what changed.


git log --oneline

Shows commit history in a short format.


git log --oneline


Commit History Flow

Each commit builds on the previous one.


git diff

Shows changes that are not committed yet.


git diff

Useful for reviewing changes before committing.


git revert <commit-hash>

Safely undoes a commit by creating a new commit.


git revert 05a7dd3


git reset --hard <commit-hash>

Moves the project back to a specific commit.


git reset --hard 05a7dd3

This removes all changes after that commit.


git branch

Lists available branches.


git branch

Branches help manage parallel development.


A Basic Git Workflow From Scratch


git init
git status
git add .
git commit -m "initial commit"
git log --oneline

This is the most common Git workflow used in real projects.

Version Control Systems

Part 2 of 3

This series explains Version Control Systems and Git from first principles. It covers why version control is necessary, how to use Git effectively as a beginner, and how Git works internally through the .git folder and object model

Up next

Why Version Control Exists: The Pendrive Problem

Why Version Control Exists Version control exists because software development stopped being a one person job a long time ago. As projects grew bigger more developers started working on the same codebase and that’s when problems began. Teams needed a...