# Inside Git: How It Works and the Role of the .git Folder

**Why does the** .git folder remain compact even after 1000 commits? How does Git efficiently track every change?

## **The** `.git` Folder: Complete Project History

**A single folder contains everything needed to reconstruct your entire project history.**

Running `git init` creates the hidden `.git` directory, which stores code history, branches, commits, and configuration.

**Practical application:** Copy only the `.git` folder to another machine and run `git checkout` to restore your complete project.

## `.git` **Directory Structure**

```bash
text.git/
├── objects/     # File content and history storage
├── refs/        # Branch and tag references
├── index        # Staging area snapshot
├── HEAD         # Current branch pointer
└── config       # Repository configuration
```

The `objects/` directory stores all content as compressed, hashed objects.

## **Git Objects**

### **Blob: File Content Storage**

A blob represents the raw content of a single file, independent of its name or path.

**Key characteristics:**

* Stores only file data (not filename or path)
    
* Identical content across files shares one blob
    
* Created during `git add`
    

```bash
texthello.txt (version 1) → blob abc123
identical.txt         → blob abc123 (shared)
hello.txt (modified)  → blob def456
```

### **Tree: Directory Structure**

A tree represents a directory and its contents, mapping project structure.

**Tree contents:**

* File names and permissions
    
* Directory names
    
* References to blobs or child trees
    

```bash
textproject-root (tree xyz789)
├── src/ (tree uvw123)
│   └── app.component.ts (blob abc123)
└── styles.css (blob def456)
```

### **Commit: Labeled Project Snapshot**

A commit captures a complete project state with metadata.

**Commit structure:**

* Reference to root tree
    
* Parent commit reference(s)
    
* Author name and timestamp
    
* Commit message
    

```bash
textCommit feedface (2026-01-10)
├── Tree: xyz789
├── Parent: cafebabe
├── Author: Your Name
└── Message: "Implement login feature"
```

Branches are simply references pointing to commit hashes:

```bash
textrefs/heads/main → feedface
refs/heads/feature → deadbeef
```

## **The** `git add` and `git commit` Workflow

```bash
 Modify app.js
2. git add app.js
   ↓ Creates blob abc123 → .git/objects/12/abc123
   ↓ Updates staging index
3. git commit -m "Add login feature"
   ↓ Creates tree xyz789 (directory structure)
   ↓ Creates commit feedface (project snapshot)
   ↓ Updates main branch reference
```

Commit chain visualization:

```bash
text∅ → commit1(tree1) → commit2(tree2) → main@HEAD(tree3)
```

## **Space Efficiency: Content Deduplication**

```bash
textNaive approach: 1000 commits × 100 files = 100,000 files
Git reality: ~500 unique blobs (incremental changes)
```

**Deduplication occurs across:**

* Unchanged files across commits
    
* Identical files across branches
    
* Duplicate content across projects
    

## **Content Addressing with SHA-1 Hashes**

Every Git object uses a 40-character SHA-1 hash derived from its content.

```bash
textIdentical content → Identical hash
Single character change → Completely different hash
```

**Benefits:**

* **Integrity:** Tampering invalidates all dependent objects
    
* **Deduplication:** Automatic content sharing
    
* **Universality:** Object references work across repositories
    

## **Storage Organization**

```bash
text.git/objects/
├── 12/              # Objects with hash prefix "12"
│   └── abc123...    # app.js blob
├── ab/
│   └── def456...    # styles.css blob
└── pack/            # Compressed object bundles
```
