<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Keerthi Vardhan</title>
    <description>The latest articles on DEV Community by Keerthi Vardhan (@keerthivardhan1).</description>
    <link>https://dev.to/keerthivardhan1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F961975%2Faade0acf-7244-4758-a019-2a37722d3b2c.jpeg</url>
      <title>DEV Community: Keerthi Vardhan</title>
      <link>https://dev.to/keerthivardhan1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/keerthivardhan1"/>
    <language>en</language>
    <item>
      <title>Mini-git, Understanding How Files Are Stored in Git Objects</title>
      <dc:creator>Keerthi Vardhan</dc:creator>
      <pubDate>Wed, 21 Aug 2024 06:56:30 +0000</pubDate>
      <link>https://dev.to/keerthivardhan1/mini-git-understanding-how-files-are-stored-in-git-objects-5bfb</link>
      <guid>https://dev.to/keerthivardhan1/mini-git-understanding-how-files-are-stored-in-git-objects-5bfb</guid>
      <description>&lt;p&gt;Yesterday, I set out to implement one of Git's core functionalities on my own—specifically, how files are stored, what Git objects are, and the processes of hashing and compressing. It took me 4 hours to develop, and in this article, I'll walk you through my thought process and approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Happens When You Commit a File?
&lt;/h2&gt;

&lt;p&gt;When you commit a file in Git, several important steps occur under the hood:&lt;/p&gt;

&lt;h2&gt;
  
  
  File Compression:
&lt;/h2&gt;

&lt;p&gt;The content of the file is compressed using a zlib algorithm to reduce its size. This compressed content is what gets stored in the Git object database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hash Calculation:
&lt;/h2&gt;

&lt;p&gt;A unique SHA-1 hash is generated from the compressed file content. This hash serves as the identifier for the file in the Git object database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Storing the Object:
&lt;/h2&gt;

&lt;p&gt;The object file is stored in the .mygit/objects directory, organized by the first two characters of the hash. This structure makes it easier to manage and retrieve objects efficiently.&lt;br&gt;
Updating Commit Information:&lt;/p&gt;

&lt;p&gt;To demonstrate how files are stored in git.&lt;br&gt;
I have implemented commit functionality, taking one file in to consideration&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For every file, I have calculated hash&lt;/li&gt;
&lt;li&gt;Inside objects folder, new folder is created with name equal to first two characters of hash.&lt;/li&gt;
&lt;li&gt;And a file is created inside that folder with remaining hash as name.(this file stores the compressed format of committed file)&lt;/li&gt;
&lt;li&gt;Detected changes by comparing newly calculated hash and last calculated hash of the file&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Detecting Changes
&lt;/h2&gt;

&lt;p&gt;I implemented this algorithm based on my own approach, but Git uses more efficient algorithms for these operations.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Extracted array of lines from oldContent and newContent&lt;/li&gt;
&lt;li&gt;Created a Map to store line as key and index as value&lt;/li&gt;
&lt;li&gt;Created two new arrays to store indexes of common lines in oldContent and newContent
4.eg:
OldCommonarray = [0 , 3] then deleted lines will be [1,2]&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://github.com/Keerthivardhan1/Mini-Git" rel="noopener noreferrer"&gt;GitHub Repo&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/keerthi-vardhan-tekulapelli-7064a6245/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks a lot for you time.&lt;/p&gt;

</description>
      <category>git</category>
      <category>javascript</category>
      <category>node</category>
      <category>github</category>
    </item>
  </channel>
</rss>
