<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[vikash's blog]]></title><description><![CDATA[vikash's blog]]></description><link>https://vikashs-blog.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 23:59:02 GMT</lastBuildDate><atom:link href="https://vikashs-blog.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Inside Git: How It Works and the Role of the .git Folder:]]></title><description><![CDATA[When I first learned Git, I only memorized commands like git init and git commit.They worked, but I didn’t really understand what Git was doing behind the scenes.
So I decided to look inside Git itself — especially the mysterious .git folder.That com...]]></description><link>https://vikashs-blog.hashnode.dev/inside-git-how-it-works-and-the-role-of-the-git-folder</link><guid isPermaLink="true">https://vikashs-blog.hashnode.dev/inside-git-how-it-works-and-the-role-of-the-git-folder</guid><category><![CDATA[GitHub]]></category><category><![CDATA[Git]]></category><category><![CDATA[insidegit]]></category><category><![CDATA[vcs]]></category><category><![CDATA[version control systems]]></category><dc:creator><![CDATA[Vikash Kumar]]></dc:creator><pubDate>Fri, 16 Jan 2026 04:25:49 GMT</pubDate><content:encoded><![CDATA[<p>When I first learned Git, I only memorized commands like <strong>git init</strong> and <strong>git commit.</strong><br />They worked, but I didn’t really understand <strong>what Git was doing behind the scenes</strong>.</p>
<p>So I decided to look inside Git itself — especially the mysterious <strong>.git folder.</strong><br />That completely changed how I understand Git.</p>
<p>This blog explains <strong>how Git works internally</strong>, in simple words, without heavy theory.</p>
<hr />
<h2 id="heading-how-git-works-internally-big-picture">How Git Works Internally (Big Picture):</h2>
<p><strong>Git:</strong> It is an open-source version control tool that is used by programmers for controlling versions of their codebase and collaborate with others. You can install your own Git server on your server. Most cloud providers also give it.</p>
<p>Instead of saving file differences line by line, Git:</p>
<ul>
<li><p>Takes snapshots of your project</p>
</li>
<li><p>Stores them efficiently</p>
</li>
<li><p>Connects them together using history</p>
</li>
</ul>
<p>Everything Git does is powered by what’s inside the <strong>.git folder.</strong></p>
<hr />
<h3 id="heading-understanding-the-git-folder">Understanding the .git Folder</h3>
<p>When we run:</p>
<pre><code class="lang-bash">git init
</code></pre>
<p>Git creates a hidden folder called <strong>.git folder.</strong></p>
<p>This folder is brain of Git.</p>
<p>If this folder is deleted, Git forgets everything about the project.</p>
<p><strong>Why the</strong> .git <strong>folder exists:</strong></p>
<ul>
<li><p>To store project history</p>
</li>
<li><p>To store commits</p>
</li>
<li><p>To track branches and HEAD</p>
</li>
<li><p>To ensure data integrity</p>
</li>
</ul>
<p>Your actual code lives <strong>outside</strong> .git,<br />but Git’s memory lives <strong>inside</strong> .git folder.</p>
<hr />
<p><strong>Inside</strong> .git**, some important parts are:**</p>
<ul>
<li><p><strong>objects/</strong> → stores all Git data</p>
</li>
<li><p><strong>refs/</strong> → stores branch pointers</p>
</li>
<li><p><strong>HEAD</strong> → points to current branch</p>
</li>
<li><p><strong>index</strong> → staging area data</p>
</li>
</ul>
<p>You usually never edit these files manually — Git handles them for you.</p>
<hr />
<h2 id="heading-git-objects-blob-tree-and-commit">Git Objects: Blob, Tree, and Commit:</h2>
<p>Git stores everything as <strong>objects</strong>.<br />There are only <strong>three main object types</strong>, and understanding them builds a strong mental model.</p>
<hr />
<h3 id="heading-1-blob-file-content">1. Blob (File Content):</h3>
<p>A <strong>blob</strong> stores the <strong>content of a file</strong>.</p>
<p>Important thing I learned:</p>
<ul>
<li><p>Blob does <strong>not</strong> store file name.</p>
</li>
<li><p>Blob only stores file data.</p>
</li>
</ul>
<p>If two files have the same content, Git stores <strong>only one blob</strong>.</p>
<p>Question: How to store only one blob?</p>
<p>Answer:</p>
<p>If two files have exactly the same content, Git calculates the same hash for both and stores <strong>only one blob</strong>.<br />Both files then simply point to that same blob, which helps Git save space and work efficiently.</p>
<h3 id="heading-2-tree-folder-structure">2. Tree (Folder Structure)</h3>
<p>A <strong>tree</strong> represents a directory.</p>
<p>It stores:</p>
<ul>
<li><p>File names.</p>
</li>
<li><p>Folder structure.</p>
</li>
<li><p>Links to blobs and other trees.</p>
</li>
</ul>
<p>Tree objects tell Git <strong>how files are organized</strong>.</p>
<h3 id="heading-3-commit-snapshot">3. Commit (Snapshot)</h3>
<p>A <strong>commit</strong> connects everything.</p>
<p>A commit contains:</p>
<ul>
<li><p>A reference to a tree</p>
</li>
<li><p>Parent commit(s)</p>
</li>
<li><p>Author and message</p>
</li>
<li><p>Timestamp</p>
</li>
</ul>
<p>I<strong>f I talk to in simple words:</strong></p>
<blockquote>
<p>A commit = snapshot + history + message.</p>
</blockquote>
<hr />
<h2 id="heading-relationship-between-commit-tree-and-blob">Relationship Between Commit, Tree, and Blob</h2>
<p>Mental model I use: The chain of command</p>
<p>you can visualise the relationship as a hierarchy or a “chain of pointers” They do not talk to each other directly. they need a middleman called a Tree.</p>
<p>Git internally works using <strong>three main object types</strong>:</p>
<ul>
<li><p><strong>Blob</strong> → file content.</p>
</li>
<li><p><strong>Tree</strong> → folder structure.</p>
</li>
<li><p><strong>Commit</strong> → snapshot + history.</p>
</li>
</ul>
<p>Each object has a specific role, and together they form Git’s core design.</p>
<h2 id="heading-blob-the-actual-file-content">Blob: The Actual File Content</h2>
<p>A <strong>blob</strong> stores the <strong>content of a file</strong>, nothing else.</p>
<p>Important things about blobs:</p>
<ul>
<li><p>A blob does <strong>not</strong> store file names</p>
</li>
<li><p>A blob does <strong>not</strong> store folder paths</p>
</li>
<li><p>It only stores raw data (text or binary)</p>
</li>
</ul>
<p>If two files have the same content, Git creates <strong>only one blob</strong> and both files point to it.<br />This is how Git saves space and avoids duplication.</p>
<h2 id="heading-tree-the-folder-structure">Tree: The Folder Structure</h2>
<p>A <strong>tree</strong> represents a <strong>directory (folder)</strong>.</p>
<p>A tree object stores:</p>
<ul>
<li><p>File names.</p>
</li>
<li><p>Folder names.</p>
</li>
<li><p>Permissions.</p>
</li>
<li><p>Links to blobs and other trees.</p>
</li>
</ul>
<p>Think of a tree as a <strong>map</strong> that tells Git:</p>
<ul>
<li><p>Which files exist.</p>
</li>
<li><p>Where they are located.</p>
</li>
<li><p>Which blob belongs to which file name.</p>
</li>
</ul>
<h2 id="heading-commit-the-snapshot-and-history">Commit: The Snapshot and History</h2>
<p>A <strong>commit</strong> is the top-level object.</p>
<p>A commit stores:</p>
<ul>
<li><p>A reference to one tree (root directory).</p>
</li>
<li><p>Reference to parent commit(s).</p>
</li>
<li><p>Author information.</p>
</li>
<li><p>Commit message.</p>
</li>
<li><p>Timestamp.</p>
</li>
</ul>
<p>A commit does <strong>not</strong> store files directly.<br />It simply points to a tree, which then points to blobs.</p>
<hr />
<h2 id="heading-how-they-work-together-step-by-step">How They Work Together (Step by Step)</h2>
<p>When I make changes and commit:</p>
<ol>
<li><p>Git creates <strong>blobs</strong> for file contents</p>
</li>
<li><p>Git creates <strong>trees</strong> to describe folder structure</p>
</li>
<li><p>Git creates a <strong>commit</strong> pointing to the root tree</p>
</li>
<li><p>Git links the commit to previous commit(s)</p>
</li>
</ol>
<p>So internally, Git looks like this:</p>
<p><strong>The connection:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1767295105530/1c59a5ed-a2bf-47f9-93a1-8a20d855d963.png" alt class="image--center mx-auto" /></p>
<hr />
<h2 id="heading-why-git-uses-this-design">Why Git Uses This Design</h2>
<p>This design gives Git several advantages:</p>
<h3 id="heading-1-efficiency">1. Efficiency</h3>
<ul>
<li><p>Same content = same blob</p>
</li>
<li><p>No unnecessary duplication</p>
</li>
</ul>
<h3 id="heading-2-safety">2. Safety</h3>
<ul>
<li><p>Nothing is overwritten</p>
</li>
<li><p>Old data stays safe as long as it’s referenced</p>
</li>
</ul>
<h3 id="heading-3-speed">3. Speed</h3>
<ul>
<li><p>Git only creates new objects when content changes</p>
</li>
<li><p>Unchanged parts are reused</p>
</li>
</ul>
<h3 id="heading-4-strong-history">4. Strong History</h3>
<ul>
<li><p>Commits form a linked timeline</p>
</li>
<li><p>Every snapshot is preserved</p>
</li>
</ul>
<hr />
<h2 id="heading-real-world-example">Real-World Example</h2>
<p>Suppose I change only one file in a project with 100 files.</p>
<p>Git will:</p>
<ul>
<li><p>Create <strong>one new blob</strong> (for the changed file)</p>
</li>
<li><p>Reuse existing blobs for unchanged files</p>
</li>
<li><p>Create a new tree and commit</p>
</li>
</ul>
<p>So Git is not copying the whole project again — it’s being smart.</p>
<hr />
<h2 id="heading-the-mental-model-that-clicked-for-me">The Mental Model That Clicked for Me</h2>
<p>Instead of thinking:<br />“Git saves files”</p>
<p>I think:<br />“Git stores objects and links them”</p>
<ul>
<li><p><strong>Blobs</strong> store data</p>
</li>
<li><p><strong>Trees</strong> organize data</p>
</li>
<li><p><strong>Commits</strong> record snapshots</p>
</li>
</ul>
<p>Once I understood this, Git commands stopped feeling random.</p>
<hr />
<h2 id="heading-final-thought">Final Thought</h2>
<p>Git is not magic.<br />It’s a <strong>content-addressed database</strong> with a brilliant design.</p>
<p>If you understand how <strong>commit → tree → blob</strong> works.</p>
<hr />
<h2 id="heading-how-git-tracks-changes">How Git Tracks Changes</h2>
<p>When I first started using Git, I thought it tracked files the same way my computer does by name and location.<br />But Git actually works in a much smarter way.</p>
<p>Git tracks <strong>changes in content</strong>, not just files.</p>
<hr />
<h3 id="heading-git-tracks-content-not-file-names">Git Tracks Content, Not File Names</h3>
<p>Every time I change a file, Git looks at <strong>what changed inside the file</strong>, not the file name itself.<br />If the content is different, Git treats it as a new version.</p>
<p>If two files have exactly the same content, Git stores that content <strong>only once</strong> and simply points both files to it.<br />This makes Git efficient and avoids duplication.</p>
<hr />
<h3 id="heading-the-role-of-the-git-folder">The Role of the .git Folder</h3>
<p>All of Git’s tracking logic lives inside a hidden folder called .git.</p>
<p>This folder exists to:</p>
<ul>
<li><p>Store the complete history of the project</p>
</li>
<li><p>Keep records of commits and branches</p>
</li>
<li><p>Track changes safely and reliably</p>
</li>
</ul>
<hr />
<h3 id="heading-what-happens-when-i-use-git-add-ltfile-namegt">What Happens When I use git add &lt;file name&gt;</h3>
<p>When I run git add, Git does not save a version immediately.<br />Instead, it prepares the changes.</p>
<p>Internally:</p>
<ul>
<li><p>Git reads the file content</p>
</li>
<li><p>Converts it into a <strong>blob object</strong></p>
</li>
<li><p>Stores it inside the .git folder</p>
</li>
<li><p>Marks it as ready in the staging area</p>
</li>
</ul>
<p>This step allows me to carefully choose what should go into the next version.</p>
<hr />
<h3 id="heading-what-happens-when-i-use-git-commit">What Happens When I Use git commit</h3>
<p>When I run git commit, Git creates a permanent snapshot.</p>
<p>Internally:</p>
<ul>
<li><p>Git groups staged files into a structure</p>
</li>
<li><p>Saves a snapshot of the project</p>
</li>
<li><p>Links it to previous snapshots</p>
</li>
<li><p>Moves the project history forward</p>
</li>
</ul>
<p>This is how Git builds a clear and reliable timeline of changes.</p>
<hr />
<h3 id="heading-how-git-ensures-safety-using-hashes">How Git Ensures Safety Using Hashes</h3>
<p>Git uses <strong>hash values</strong> to identify every piece of data it stores.</p>
<p>These hashes:</p>
<ul>
<li><p>Are based on content</p>
</li>
<li><p>Change if content changes</p>
</li>
<li><p>Guarantee data integrity</p>
</li>
</ul>
<p>Because of this, Git can detect corruption and ensure history is trustworthy.</p>
<hr />
<p><strong>internal workflow of git add and git commit:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1767297370982/8b15e5ba-b04a-461e-8957-8f68fef0c5af.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item></channel></rss>