Tejas GK

Behind the Scenes: How Git Works Under the Hood

Tejas GK| (1y ago)

6 min read
Behind the Scenes: How Git Works Under the Hood

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Behind the Scenes: How Git Works Under the Hood</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333; max-width: 900px; margin: 0 auto; padding: 20px; } h1 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; } h2 { color: #2980b9; margin-top: 30px; } h3 { color: #16a085; } code { background-color: #f8f9fa; padding: 2px 4px; border-radius: 4px; font-family: 'Courier New', Courier, monospace; } pre { background-color: #f8f9fa; padding: 10px; border-radius: 4px; overflow-x: auto; } .toc { background-color: #f0f8ff; padding: 15px; border-radius: 5px; margin-bottom: 20px; } .toc ul { padding-left: 20px; } .toc li { margin-bottom: 5px; } .highlight { background-color: #fffde7; padding: 15px; border-left: 4px solid #ffd600; margin: 20px 0; } .myth { background-color: #ffebee; padding: 15px; border-left: 4px solid #f44336; margin: 20px 0; } .reality { background-color: #e8f5e9; padding: 15px; border-left: 4px solid #4caf50; margin: 20px 0; } </style></head><body> <h2>Introduction</h2> <p>Git is one of the most widely used version control systems in the world, powering everything from small personal projects to massive open-source collaborations like the Linux kernel. While many developers use Git daily, few truly understand how it works behind the scenes.</p> <p>In this in-depth guide, we'll explore Git's internal architecture, how it stores data, and the mechanisms that make version control efficient and reliable. By the end, you'll have a solid understanding of Git's core concepts, allowing you to use it more effectively and troubleshoot issues with confidence.</p>
<div class="toc"> <h3>Table of Contents</h3> <ul> <li><a href="#what-is-git">What is Git?</a></li> <li><a href="#internal-architecture">Git's Internal Architecture</a></li> <li><a href="#stores-data">How Git Stores Data</a></li> <li><a href="#branching-merging">Branching and Merging Internals</a></li> <li><a href="#head-pointer">The Role of the HEAD Pointer</a></li> <li><a href="#refs">The Refs: Branches and Tags</a></li> <li><a href="#workflow">The Git Workflow Explained</a></li> <li><a href="#networking">Networking in Git</a></li> <li><a href="#gc">Git's Garbage Collection</a></li> <li><a href="#misconceptions">Common Git Internals Misconceptions</a></li> <li><a href="#advanced">Advanced Git Internals</a></li> <li><a href="#conclusion">Conclusion</a></li> </ul> </div>
<h2 id="what-is-git">1. What is Git?</h2> <h3>A Brief History</h3> <p>Git was created in 2005 by Linus Torvalds, the creator of Linux, after a licensing dispute with BitKeeper, the version control system used for Linux kernel development at the time. Torvalds designed Git with three key goals in mind:</p> <ul> <li><strong>Speed</strong> – Fast operations even on large codebases.</li> <li><strong>Distributed Model</strong> – Every developer has a full repository history.</li> <li><strong>Integrity</strong> – Strong safeguards against data corruption.</li> </ul>
<h3>Why Git is Different</h3> <p>Unlike centralized version control systems (e.g., SVN), Git is <strong>distributed</strong>, meaning every developer has a full copy of the repository, including its entire history. This allows offline work and reduces reliance on a central server.</p> <p>Git also differs in how it <strong>stores data</strong>. Instead of tracking file changes as deltas (like SVN), Git takes snapshots of the entire file system at each commit, making operations like branching and merging extremely efficient.</p>
<h2 id="internal-architecture">2. Git's Internal Architecture</h2> <p>Git operates using three main areas:</p> <ol> <li><strong>The Git Directory (Repository)</strong> – Where Git stores all metadata and object databases.</li> <li><strong>The Working Directory</strong> – The local files you edit.</li> <li><strong>The Staging Area (Index)</strong> – A temporary area where changes are prepared before committing.</li> </ol>
<h3>The Git Directory (<code>.git</code> folder)</h3> <p>This is where Git stores everything it needs to manage the repository:</p> <ul> <li><strong>Objects database</strong> (blobs, trees, commits, tags)</li> <li><strong>Refs</strong> (branches, tags, remote tracking)</li> <li><strong>HEAD pointer</strong> (current branch/commit)</li> <li><strong>Configuration files</strong> (<code>config</code>, <code>hooks</code>, etc.)</li> </ul>
<h3>The Working Directory</h3> <p>This is your project's file system. When you modify files, Git detects changes between the working directory and the staging area.</p>
<h3>The Staging Area (Index)</h3> <p>Before committing, changes must be staged using <code>git add</code>. The staging area acts as a checkpoint, allowing selective commits.</p>
<h2 id="stores-data">3. How Git Stores Data</h2> <h3>Objects: Blobs, Trees, Commits, and Tags</h3> <p>Git's data model is built around four key objects:</p> <ol> <li><strong>Blob</strong> – Stores file contents (binary data).</li> <li><strong>Tree</strong> – Represents directories, listing blobs and subtrees.</li> <li><strong>Commit</strong> – Points to a tree (snapshot), contains author info, and links to parent commits.</li> <li><strong>Tag</strong> – A named reference to a commit (used for releases).</li> </ol>
<h3>The Hashing Mechanism (SHA-1)</h3> <p>Every object in Git is identified by a <strong>unique 40-character SHA-1 hash</strong>, computed from its content. This ensures:</p> <ul> <li><strong>Data integrity</strong> (any change alters the hash).</li> <li><strong>Efficient storage</strong> (duplicate files share the same blob).</li> </ul>
<h3>The Object Database</h3> <p>All objects are stored in <code>.git/objects/</code>:</p> <ul> <li>First two characters → Directory name.</li> <li>Remaining 38 → Filename.</li> </ul> <p>Example: <code>a1b2c3...</code> → <code>.git/objects/a1/b2c3...</code></p>
<h3>Packfiles and Compression</h3> <p>Over time, Git compresses objects into <strong>packfiles</strong> to save space. The <code>git gc</code> (garbage collection) command handles this.</p>
<h2 id="branching-merging">4. Branching and Merging Internals</h2> <h3>What is a Branch?</h3> <p>A branch is just a <strong>pointer to a commit</strong>. Creating a branch (<code>git branch feature</code>) adds a new ref in <code>.git/refs/heads/</code>.</p>
<h3>How Git Handles Merges</h3> <ul> <li><strong>Fast-forward merge</strong> – If no diverging history, HEAD moves forward.</li> <li><strong>Three-way merge</strong> – Combines changes from two branches using a common ancestor.</li> </ul>
<h3>Conflict Resolution</h3> <p>When Git can't auto-merge, it marks conflicts in files, requiring manual resolution.</p>
<h2 id="head-pointer">5. The Role of the HEAD Pointer</h2> <ul> <li><strong>HEAD</strong> points to the current commit (usually via a branch).</li> <li><strong>Detached HEAD</strong> occurs when you checkout a commit directly (not a branch).</li> </ul>
<h2 id="refs">6. The Refs: Branches and Tags</h2> <ul> <li><strong>Branches</strong> are mutable refs that move with new commits.</li> <li><strong>Tags</strong> are immutable (lightweight tags are just refs, annotated tags are full objects).</li> </ul>
<h2 id="workflow">7. The Git Workflow Explained</h2> <ol> <li>Modify files in <strong>working directory</strong>.</li> <li><code>git add</code> stages changes in the <strong>index</strong>.</li> <li><code>git commit</code> creates a new commit object from the index.</li> </ol>
<h2 id="networking">8. Networking in Git</h2> <ul> <li><code>git clone</code> downloads the entire repository.</li> <li><code>git fetch</code> retrieves remote changes.</li> <li><code>git push</code> uploads local commits.</li> </ul>
<h2 id="gc">9. Git's Garbage Collection</h2> <ul> <li>Removes unreachable objects.</li> <li>Runs automatically (<code>git gc</code>).</li> </ul>
<h2 id="misconceptions">10. Common Git Internals Misconceptions</h2> <div class="myth"> <p><strong>Myth:</strong> Git stores diffs.</p> </div> <div class="reality"> <p><strong>Reality:</strong> Git stores snapshots.</p> </div>
<div class="myth"> <p><strong>Myth:</strong> Deleting a branch deletes commits.</p> </div> <div class="reality"> <p><strong>Reality:</strong> Only the branch pointer is removed; commits remain until garbage collected.</p> </div>
<h2 id="advanced">11. Advanced Git Internals</h2> <ul> <li><strong>Reflog</strong> – Logs all reference changes (<code>git reflog</code> helps recover lost commits).</li> <li><strong>Reset vs. Rebase</strong> – <code>reset</code> moves HEAD, <code>rebase</code> rewrites history.</li> </ul>
<h2 id="conclusion">12. Conclusion</h2> <p>Understanding Git's internals makes you a more effective developer. You'll debug issues faster, use advanced commands confidently, and appreciate Git's elegant design.</p>
<h3>Further Learning Resources</h3> <ul> <li><a href="https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain" target="_blank">Git Internals - Git SCM Book</a></li> <li><a href="https://git-scm.com/book/en/v2" target="_blank">Pro Git (Free eBook)</a></li> <li><a href="https://jwiegley.github.io/git-from-the-bottom-up/" target="_blank">Git from the Bottom Up</a></li> </ul>