Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
โ€ข6 min read
Git for Beginners: Basics and Essential Commands

Let's first discuss a situation before understanding what is git ......

Imagine you are writing a school project. Every day, you make changes and save a new copy:

  • Project_Final.docx

  • Project_Final_V2.docx

  • Project_Final_Real_Final.docx

  • Project_Final_Last_Final.docx ๐Ÿ˜…

After sometimes suppose your teammates want to check what changes you have done and he wants to add feature in the project so it will be little difficult to track these changes . In the same way developers face the problem while writing the code . It gets difficult to track the changes and share the changes with your friends or teammates and that's where git comes in role

Git helps developers track changes, save different versions of their code, and work with others without losing any work.

In this blog, we will learn Git in very simple language and understand the most important Git commands with practical

what is Git ?

Git is nothing but a kind of software installed inside your folder to track the changes made by you . And in programming language this is known as
"Distributed version Control System".
Here ,
Version control means keeping track of every change made to a project.
Distributed means Every teammate or friends gets a complete copy of the project history on their own computer.

Thus in simple terms to tell git is distributed version control system which is used to track changes, save project history, and work together efficiently.

Why Git is used ?

  • Time travel: Revert files or entire projects back to previous stable states if something breaks.

  • Seamless collaboration: Allow thousands of developers to work on the exact same files simultaneously without overwriting each other's code.

  • Isolated experimentation: Create independent "branches" to test new features or fix bugs safely away from the main, working codebase.

  • Full offline capability: Work locally on your machine with a complete copy of the project history, meaning internet connectivity is not required to commit changes.

  • Accountability trail: Keep a clear log of who made what change, when they made it, and why they made it.

  • Data integrity: Cryptographically secure every change using checksums, preventing hidden data loss or file corruption.

Git basics and core terminologies

Now before stating with git commands you need understand some basic terms .
This will make it easier for you understand the commands . Let us start then......

1. Repository (Repo)

A Repository, often called a Repo, is a folder that Git uses to track and manage files.

It contains:

  • Project files

  • Git history

  • Commit records

  • Branch information

Think of a repository as a digital storage space where Git keeps your project's complete history.

2. Staging Area

The Staging Area is a temporary place where changes are prepared before being committed.
Git allows you to choose which changes should be included in the next commit.
Think of it like as a online shopping cart

  • Working Directory โ†’ Items in the store

  • Staging Area โ†’ Items in your cart

  • Commit โ†’ Checkout

3.Commit

Commit is a saved snapshot of your project at a specific point in time.
Each commit records:

  • The changes made

  • The author

  • The date and time

  • A commit message

This creates a checkpoint that you can return to later if needed.

4. Branch

A Branch is like a separate copy of your project where you can try new ideas without affecting the original project. It is like Separate workspace for development

Imagine you are writing a school project.
You have completed the main project and submitted the first draft. Now, you want to add some new sections and improve the design.

Instead of changing the original project directly, you make a photocopy and work on that copy.

  • Original Project โ†’ Main Branch

  • Photocopy for changes โ†’ New Branch

If the changes look good, you copy them back into the original project.
That's exactly how branches work in Git.

5.Head

HEAD is simply Git's way of showing where you are currently working. It is like a bookmark that tells Git where you are currently working in the project.

Common Git commands

**1.git init

**This command creates a new Git repository.

see here my project isn't being tracked yet

see on the above image my project isn't being tracked !!! so to track it I write the command git init and it Starts tracking it by making a repository ( a .git hidden file )

like this !!!

2. git add

Git doesn't automatically save changes.
First, you need to tell Git which files should be included for the changes to be tracked .
This process is called staging.

3. git commit -m " your commit message"

A commit saves the staged changes permanently.

The message should clearly describe what was changed.
Good commit messages make project history easier to understand.

so I had written a greetings in empty project file . so added to staging area and committed this change....

4. git status

This command shows the current state of your repository.

I am in the main branch and I had deleted 2 files before creating the project1 file ...

5. git log

This command shows commit history.

6. git diff

This command shows what changes you have made in your files before saving them.
Imagine you wrote an essay yesterday and made some changes today.

Now you want to compare:

  • Old version

  • New version

It acts like a teacher highlighting what was added, removed, or changed.

7. git restore

git restore is used when you want to throw away your recent changes and go back to the last saved version.

git restore = "Remove my unsaved changes."

8. git reset --hard 'commit id'

It is used to unstage files or move back to an earlier commit.

This were the some basic commands which we need understand when we start learning git

THE WORKFLOW :

For more deep dive you can visit : https://youtu.be/q8EevlEpQ2A?si=cqhIs1arH9-BzB14

**THANK YOU !!!

**