Skip to main content

Command Palette

Search for a command to run...

Git Basics and Essential Commands

Updated
4 min read

What is Git ?

Git is the tool that helps you to manage changes in you code. It helps you to keep track of every little changes to file. you can always look back at previous versions or undo mistakes if needed. Every developer gets a full copy of the project, including its history, on their own machine. This means you can work offline, experiment freely, and still keep everything organized.

Why Git is Used ?

Git is used because this solve many problems of the developers :

  • Collaboration → Multiple people work on same file at same time without over write each other code.

  • It keep full history of the code what changes are we made in our code

  • It make team work easy and manageable

  • Allow Experimenting without make any change in main code

  • It helps to fix mistakes by role back changes.

Git Basics and Core Terminologies

Git basics : Basics steps of the Git.

Repository (Repo) : A repository is a project folder that Git tracks. It contains your code and the entire change history.

Commit : A commit is a snapshot of your code at a specific point in time. Each commit has a message explaining what changed.

Branch : A branch is an independent line of development. You can create branches to add features or fix bugs without touching the main code.

HEAD : HEAD is a pointer that tells Git which commit or branch you are currently working on.

Core Terminologies

Git Workflow (Very Important)

Git works in three main stages:

Working Directory

This is your normal project folder on your laptop.

  • You create files, edit code, delete things, rename files.

  • Git is watching, but nothing is saved yet.

  • Files here can be:

    • Untracked: Git has never seen them before.

    • Modified: Git knows the file, but you changed it.

Example:
You open main.py and add a new function.
At this point, the change exists only on your machine. If your system crashes, Git has nothing saved.

Staging Area (Index)

This is the most important and most misunderstood part.

The staging area is like a preview of the next commit.

  • You choose exactly which changes should be saved.

  • Nothing is permanent yet.

Repository (Commit History)

This is where Git permanently stores changes.

When you commit:

  • Git takes everything from the staging area

  • Saves it as a snapshot

  • Assigns it a unique commit ID

  • Adds your message describing what changed

A simple real-life Example:

  • Working Directory: Writing rough notes

  • Staging Area: Highlighting what goes into the final answer

  • Repository: Submitting the exam paper

Common Git Commands

Initialize a Repository :

git init

Creat new git repositort in the current folder.

Check Repository Status :

git status

Shows modified files, staged files, and what Git is tracking.

Add Files to Staging Area :

git add filename

or

git add .

Stages one file or all changes.

Commit Changes :

git commit -m "Your message here"

Saves staged changes with a clear message.

View Commit History

git log

Shows all previous commits in order.

git diff

git diff shows what has changed in your files.
It compares different versions of your code and highlights the exact lines added, removed, or modified.

git diff

Shows changes in the working directory that are not yet staged.

What you’ll see:

  • Lines starting with - were removed

  • Lines starting with + were added

git add <file_name>

git add tells Git which changes you want to include in the next commit.
It moves files from the working directory to the staging area.

Basic usage

git add index.html

Stages only index.html.

git revert

git revert is the safe way to undo a commit.
It does not delete history. Instead, it creates a new commit that reverses the changes of an earlier one.

git revert <commit-hash>

git reset

git reset moves HEAD backward to a previous commit.
Unlike revert, it can rewrite history, so it must be used carefully.

There are three common modes.

1. Soft reset

git reset --soft <commit-hash>
  • Keeps changes staged

  • Moves HEAD only

Used when you want to fix a commit message or regroup commits.

2. Mixed reset (default)

git reset <commit-hash>
  • Keeps changes in files

  • Unstages them

Used when you added files by mistake.

3. Hard reset

git reset --hard <commit-hash>
  • Deletes commits

  • Deletes file changes

  • Very dangerous.

More from this blog