Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
7 min read
Git for Beginners: Basics and Essential Commands
H
CS undergrad | Tech enthusiast | Focusing on Web Dev • DSA • ML | Building skills for real-world impact

TL;DR

Git is a distributed version control system that helps developers track code changes, work with others, and manage different versions of a project efficiently.

Git is used to enable safe collaboration, track change history, work in parallel using branches, and recover code easily without losing work.

Git works using repositories, commits, branches, and merges to record changes, organize development, and combine work from multiple contributors.

Common Git commands like git init , git status , git add, git commit, git pull, and git push help create repositories, track changes, and sync code with others.

What is Git?

Git is a tool that helps developers save, track, and manage changes in their code over time. It is a distributed version control system , which helps us to track the changes in our code

Think of Git like a smart history book for your project.

In simpler words, every time you tell Git to save, it takes a snapshot of your project.
If something breaks later, you can always go back to a previous snapshot.

Every time you make a change and save it using Git, it remembers:

  • What changed

  • Who made the change

  • When it was made

  • Why it was made


Why Git is used?

Git is used because code is constantly changing.

It helps developers:

  • Track what changed, when, and why

  • Experiment safely without fear of breaking the main code

  • Recover older, stable versions when something goes wrong

  • Work with others without overwriting each other’s changes

Just imagine you are writing a document:

  • You make changes today

  • Tomorrow you make more changes

  • Later, something breaks and you want the old version back

Without Git, you might create files like:

project_final.doc
project_final_v2.doc
project_final_latest.doc

With Git:

  • You don’t need multiple copies

  • Git keeps all versions safely

  • You can go back to any previous version at any time


Git Basics and Core Terminologies

Before diving into Git commands, it's important to understand some basic Git terms.

  1. Repository (Repo)

A repository, often called a repo, is a project folder tracked by Git. It contains all your project files and the complete history of changes made to them.

  1. Working Directory

The working directory is where Git stores your project files, allowing you to view, edit, and work on them. Any changes you make here are not tracked until you add them to Git.

  1. Staging Area

The staging area is where you prepare changes before saving them permanently.
It lets you decide which changes to include in the next commit.

  1. Commit

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

  • Has a unique ID

  • Includes a message describing what was changed

Commits help you track your progress and return to previous versions when necessary.

  1. Branch

A branch lets you work on a new feature or fix without changing the main code.
It allows developers to work independently and safely before merging their changes.

HEAD points to your current location in the project's history.
It tells Git which branch or commit you are currently working on.


Essential Git Commands

Before using Git in real projects, you need to know some core commands. These commands help you set up a repository, track changes, save your work, and view the history.

  1. git init

  • Initializes a new Git repository in your project folder.
git init

This command:

  • Creates a hidden .git folder

  • Tells Git to start tracking the project

You run this once when starting a new project.

  1. git status

  • Shows the current state of your project.
git status

It tells you:

  • Which files are modified

  • Which files are staged

  • Which files are untracked

This is the most frequently used Git command.

  1. git add

  • Adds changes to the staging area.
git add file.txt

To add all files at once:

git add .
  1. git commit

  • Saves the staged changes permanently.
git commit -m "your message"

This command:

  • Creates a snapshot of the project

  • Requires a meaningful message describing the change

A commit is like a save point you can return to later.

  1. git log

  • Shows the history of commits.
git log

It displays:

  • Commit IDs

  • Author information

  • Date and time

  • Commit messages


Typical Git Workflow (Simple)

  1. Make changes in files

  2. Check status → git status

  3. Stage changes → git add

  4. Save changes → git commit

  5. View history → git log


Others Essential Git Commands

  1. git push

  • This command uploads your local commits to a remote repository (like GitHub).

When to use it:
After committing your changes locally and you want others to see them.

git push origin main
  • Sends your commits to the main branch on the remote repository.
  1. git pull

  • This command downloads changes from a remote repository and merges them into your local branch.

When to use it:
Before starting work, to make sure your code is up to date.

git pull origin main
  • Helps avoid conflicts by keeping your local code updated.
  1. git clone

  • This command creates a local copy of a remote repository.
git clone https://github.com/username/repository.git

Used when you want to start working on an existing project.


Git working directory → staging area → repository

  1. Working Directory

The working directory is where you write and edit your code.

  • This is your project folder on your computer

  • You create, modify, or delete files here

  • Git can see changes, but it does not save them yet

git add

  • The git add command moves changes from the working directory to the staging area.
  1. Staging Area

The staging area is a temporary holding place for changes.

  • Files here are ready to be committed

  • You can review and adjust what’s staged before saving

git commit

  • The git commit command saves staged changes permanently, creating a snapshot of your project, stores it in Git history with a message explaining the change.
  1. Repository

The repository is where Git stores all commits.

  • Contains the complete project history

  • Lives inside the .git folder

  • Allows you to:

    • Go back to older versions

    • See who changed what

    • Track progress over time


Local Git Repository Structure

When you run git init inside a project, Git creates a hidden folder called .git.
This folder is the heart of Git—it contains everything Git needs to track and manage your project.

Inside the .git folder, Git stores:

  • Commit history – all snapshots of your project

  • Branch information – details about branches like main

  • HEAD – a pointer that tells Git where you currently are

  • Internal objects and references – used by Git to manage versions

project/
 ├── your_files
 ├── .git(hidden)
        ├── objects
        ├── refs
        ├── HEAD

How Commit History Flows

Git stores changes as a chain of commits.

  • Every commit points to the previous commit

  • This creates a timeline (or history) of changes

  • HEAD always points to the latest commit in the current branch

  • When you create a new commit, HEAD moves forward to it

This is how Git knows what your “current version” is.

Commit A → Commit B → Commit C
                        ↑
                       HEAD

💡 Tip: New to Git and wondering why it was needed in the first place? Don’t miss my other blog where I break it down simply. Click Below -

Why Version Control Exists?

🔗 Connect with me:
LinkedIn | GitHub | X (Twitter)