# Git for Beginners: Basics and Essential Commands

## **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 **<mark>save, track, and manage changes</mark>** in their code over time. It is a **<mark>distributed version control system</mark>** , which helps us to track the changes in our code

Think of **Git** like a **<mark>smart history book for your project</mark>**.

In simpler words, every time you tell Git to save, it takes a <mark>snapshot </mark> 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 <mark>constantly changing</mark>.

It helps developers:

* Track what **<mark>changed</mark>**, **<mark>when</mark>**, and **<mark>why</mark>**
    
* Experiment safely without fear of breaking the main code
    
* **<mark>Recover </mark>** older, stable versions when something goes wrong
    
* Work with others without **<mark>overwriting </mark>** 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:

```bash
project_final.doc
project_final_v2.doc
project_final_latest.doc
```

**With Git**:

* You don’t need **<mark>multiple copies</mark>**
    
* Git keeps **<mark>all versions safely</mark>**
    
* You can go back to any previous version **<mark>at any time</mark>**
    

---

## Git Basics and Core Terminologies

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767948126002/705f5942-e8b8-4e5c-84e3-6397cb31e52e.png align="center")

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 **<mark>project files</mark>** and the **<mark>complete history of changes</mark>** made to them.

2. ### 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**.

3. ### 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.

4. ### Commit
    

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

* Has a **<mark>unique ID</mark>**
    
* Includes a **<mark>message</mark>** describing what was changed
    

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

5. ### Branch
    

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

6. ### HEAD
    

**HEAD** points to your current location in the project's history.  
It tells **Git** which **<mark>branch or commit</mark>** 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.
    

```bash
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.

2. ### `git status`
    

* Shows the **current state** of your project.
    

```bash
git status
```

It tells you:

* Which files are **<mark>modified</mark>**
    
* Which files are **<mark>staged</mark>**
    
* Which files are **<mark>untracked</mark>**
    

This is the most frequently used Git command.

3. ### `git add`
    

* Adds **changes** to the staging area.
    

```bash
git add file.txt
```

To add all files at once:

```bash
git add .
```

4. ### `git commit`
    

* Saves the **staged changes permanently**.
    

```bash
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.

5. ### `git log`
    

* Shows the **history of commits**.
    

```bash
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.

```bash
git push origin main
```

* Sends your commits to the `main` branch on the remote repository.
    

2. ### `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.

```bash
git pull origin main
```

* Helps avoid conflicts by keeping your local code updated.
    

3. ### `git clone`
    

* This command creates a **local copy of a remote repository**.
    

```bash
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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767949919266/efb14461-68f6-43ce-98be-d6a9f2813264.png align="center")

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**.
    

2. ## 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.
    

3. ## 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
    

```bash
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.

```bash
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?**](https://blog-git-need.hashnode.dev/)
> 
> 🔗 **Connect with me:**  
> [**LinkedIn**](https://linkedin.com/in/rajharsh4618) | [**GitHub**](https://github.com/Harsh-Raj4618) | [**X (Twitter)**](https://x.com/Harsh_Raj4618)
