# Understanding Git

## Introduction

Git is a helpful device for developers that lets them keep record of changes in their code and collaborate without any confusion. Instead of making several copies like `project_final`, `project_final_v2`, and `project_final_really_final` , Git maintains a neat, structured history of every change you do.

Git works by giving each person their own full version of a project. Because everyone holds the entire timeline locally, there is no need to rely on one main server. Work continues smoothly even without access to shared systems. Speed comes from changes being saved right where they happen. Mistakes are easier to fix since past states stay within reach. Trust builds through independence across locations.

In this article, we will cover everything: from the basics of using Git to understanding how it internally works. Initially, you will get to know the simple commands that developers use on a daily basis. Later on, we will "open the hood" and see what is happening in the . git folder, how Git stores data, and how it maintains the integrity and security of your project.

## What is Git and Why Git is used?

Git is a version control system that enables users to keep track of the changes made to their files over a period of time. This means every developer has a full copy of the project and its complete history on their own computer.

In a way, you can say Git is a clever project timeline that allows you to restore any previous version of the project without any limitations.

Git is also a Distributed Version Control System (DVCS), which implies that each developer has a copy of the entire project along with its complete history saved in their local machine. No need to stay online most of the time to commit your changes, that is why Git is fast, reliable, and powerful.

### Why it is used?

Git is used to track changes in code, collaborate with others, and keep a safe history of a project. It allows developers to go back to any previous version, experiment without fear using branches, and work together without ones work being overwritten.

## Git Basics

**Repository (Repo)**

In Git, a repository is the home of your project. Git keeps all of your files and their whole history there. Consider it your project's journal, documenting every modification you make.

**Commit**

A commit is like saving your game progress. You tell Git: “This version is important, remember it.” in simple terms, it snapshots your work at that moment of time with a describing message to represent that commit.

**Branch**

A branch is a copy of your work line. You can try new ideas without breaking the main code. you can try new features as well as try bug fixes in the line and it wont affect your main code.

**HEAD**

A link to the particular commit or branch you are currently working on. by detecting the snapshot of your project that is presently checked out in your working directory and acting as the parent for your next commit.

**Working Directory**

The working directory is the folder where you write code and edit files. It's a single version of your project, pulled from the Git repository for you to work with.

**Staging Area**

It is a layer between your working directory and your local repository. It acts as a "waiting room" where you prepare changes before committing them.

**Local vs Remote Repo**

* **Local repo** → Your Git project on your own computer
    
* **Remote repo** → The same project but stored online (like GitHub) so others can access it.
    

## Git Workflow (Big Picture)

Git can be compared to a camera that takes pictures of your project. But before the snapshot is saved forever, it passes through three rooms:

Each room has a purpose.

1. **Working Directory**
    
    * This is where you actually work.
        
    * You create files of code and edit it.
        
    * You delete or modify content
        
    
    It’s just your normal project folder.
    
2. Staging Area (Index)
    
    This is the Waiting area.
    
    It lets you:
    
    * Choose what to commit
        
    * Ignore unfinished changes
        
    * Group related changes together
        
    
    when you run:
    
    `git add index.html`
    
3. **Repository**
    
    When you run:
    
    `git commit -m "Add layout”`
    
    Git:
    
    * Takes everything from the staging area
        
    * Creates a snapshot of state
        
    * Saves it forever in the repository
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769543846955/aa853474-83b3-47f6-b4b8-21e42e24ac53.png align="center")
    

## Common Git Commands (Beginner Essentials)

1. `git init`
    
    Use: Creates a new Git repository in your project folder.
    
    It tells Git: “Start tracking this project.”
    
2. `git status`
    
    Use: Shows the current state of your project.
    
    It tells you:
    
    * Which files are modified
        
    * Which files are staged
        
    * Which files are not being tracked
        
3. `git add`
    
    Use: Moves changes from the working directory to the staging area.
    
    It prepares files for commit.
    
4. `git commit`
    
    Use: Saves a snapshot of staged changes into the repository.
    
    This becomes a permanent point in your project history.
    
5. `git log`
    
    Use: Shows the history of commits.
    
    You can see:
    
    * Who made the change
        
    * When it was made
        
    * What message was written
        
6. `git diff`
    
    Use: Shows what has changed but is not yet committed.
    
    It helps you review your work before saving it.
    
7. `git branch`
    
    Use: Shows all branches or creates a new branch.
    
    Branches let you work on features safely.
    
8. `git switch` (or git checkout)
    
    Use: Move between branches.
    
9. `git merge`
    
    Use: Combines one branch into another.
    
10. `git clone`
    
    Use: Copies an existing repository from a remote source to your system.
    
11. `git pull`
    
    Use: Gets the latest changes from the remote repository and updates your local one.
    
12. `git push`
    
    Use: Sends your committed changes to the remote repository.
    
13. `git reset` (soft intro)
    
    Use: Upstages a file without deleting your changes.
    
14. `git rm`
    
    Use: Removes a file from both your project and Git tracking.
    
15. `git config`
    
    Use: Sets your name and email for commits.
    

## A Real Developer Workflow

```plaintext
Project Folder
|
|  git init
v
[Empty Repository]
|
|  Create file: index.html
|  git status
v
[Working Directory]
|
|  git add index.html
v
[Staging Area]
|
|  git commit -m "Initial commit"
v
[Commit A]
|
|  Modify index.html
|  git add index.html
|  git commit -m "Update homepage"
v
[Commit B]
|
|  Modify again
|  git add .
|  git commit -m "Add footer"
v
[Commit C]
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769543877954/fb10af80-8f26-4551-bd26-2c158bc89f4a.png align="left")

## What is a .git folder?

The .git folder is the heart and brain of Git.

This secret folder is created by Git when you execute git init, at which point your project becomes a Git repository.

This is where Git stores all of its knowledge about your project:

* our commit history
    
* Branches
    
* Configuration
    
* Staging area
    
* All file snapshots
    

Your project files are just the body.

The .git folder is the memory, logic, and timeline.

this folder exists to:

* Store project history
    
* Track changes safely
    
* branching and merging
    
* Assure data integrity using hashes
    
* Make Git distributed (every repo has full history)
    

### Why does deleting `.git` remove Git tracking?

Because deleting `.git` is like deleting a person’s memory.

Your files will still exist:

```plaintext
index.html
app.js
style.css
```

But Git will forget the committed states, the staged data.

## How Git Stores Data (Git Objects)

Git doesn’t store your project as “files and folders” the way your operating system does.

Instead, it stores **objects**, and everything in Git is built on just three main types:

1. Blob → File Content
    

A Blob stores:

Only the content of a file, no file name, no file info.

Git saves only one blob and reuses it when two files contain same content.

Smart and effective, like a data-saver.

2. Tree → Folder Structure
    

A **Tree** object:

* Maps filenames to blobs
    
* Creates your project’s structure
    

It’s how it does:

```plaintext
src/
 ├── app.js
 └── utils.js
```

3. Commit
    

A **Commit** Points to one tree (the root directory) and Stores:

* Author
    
* Date
    
* Message
    
* Parent commits
    

This is your project’s saved state in moment.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769543920063/2d3aa65f-1edf-4d41-bdd9-c581e83d034f.png align="center")

## What Happens During `git add` and `git commit`

### git add

* **Read the file:** Git looks at what’s inside your `file.txt`.
    
* **Creates a "Blob":** Git takes that raw text and turns it into a "Blob." Think of a Blob as just a bucket of data.
    
* **Store it:** Git saves that bucket inside a secret folder: `.git/objects/`.
    
* **Get a ID (Hash):** Git looks at the content and gives it a unique ID name using math. This is the **Hash**.
    
* **Update the Index:** Git updates the "Staging Area." Now, the index knows about this specific version of your file.
    
* **Ready to go:** This tells Git: "Hey, this version of the file is ready. Let's save it forever in the next commit."
    

### git commit:

When you execute `git commit`, Git moves from tracking raw data to building a permanent record of your project.

Here is the breakdown of that process:

**1\. Organizing the Structure**

Git looks at your **Staging Area** (the index) and bundles everything into a **Tree object**.

* This tree represents your project's directory structure at that exact moment.
    
* It links your filenames to the specific **Blob hashes** (the file content) created during the `git add` phase.
    

**2\. Finalizing the Commit**

Next, Git generates a **Commit object**. This acts as a snapshot "envelope" that contains:

* A pointer to the folder structure mentioned above.
    
* Your name, the date/time, and your commit message.
    
* **The Link (Parent):** A reference to the previous commit hash, which creates the chain of history.
    

**3\. Advancing the Timeline**

now, Git updates your **HEAD**.

* The **HEAD** pointer moves from the old commit to this new one.
    

## What is a Hash?

In simple terms, a **hash** is a digital fingerprint. You give Git any piece of data like the words "hello Git" and it runs it through a math formula to generate a unique string of 40 characters (e.g., `f472d39...`).

**How Git uses Hash:**

Hashes are used by Git to identify everything:

* Blob objects (contents of files)
    
* Tree objects (folder organization)
    
* Commit items (snapshots)
    

Rather than saying:

**"This is file1.txt."**

Git say:

**"This content has the hash abc123."**

so, When Git stores something: it caculates its hash and uses it as its ID and save the object using this hash.

### Why Hashes Guarantee Integrity

Integrity means your data cannot be changed without you knowing. the hash acts like a security seal on a package. If someone modifies a file even slightly the hash changes completely. Every time Git reads your data, it recalculates the hash. If the new hash doesn't match the original, Git detects it.

### Why Hashes Guarantee Uniqueness

Uniqueness means every version of your work has its own distinct identity. Git doesn't just look at filenames; it looks at the actual content.

A tiny change like swapping a lowercase "w" for a capital "W" completely changes the hash. This creates a brand-new identity for that version.

### Why Hashes Guarantee Safety

Git history resembles a chain that has been soldered together. The hash of each commit is derived from both its own data and the hash of the previous commit. Any changes made in the past will cause all subsequent hashes to break. Git history is tamper-proof because you can't change it without everyone noticing.

## Conclusion

Git is not just a tool to save code, it is a system that helps you work smarter and safer. It keeps track of every change, protects your work, and makes teamwork easy. Once you understand both how to use Git and how it works inside, Git feels less scary and more powerful. With a little practice, it becomes one of the most helpful tools in your developer journey.

Thank you for reading..
