# How Git Functions: A Look Inside the .git Folder and Its Role ![Cover image for How Git Functions: A Look Inside the .git Folder and Its Role](https://prod-files-secure.s3.us-west-2.amazonaws.com/1daf8d7b-bd6a-4278-9213-f77aa4198ce5/36687f2a-f528-4861-b327-11a2b4b3fd94/24b46f51-a9f0-4d40-b6f7-86409fd794ed.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB4665NW6QBMF%2F20261101%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20261101T133309Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEJr%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLXdlc3QtMiJHMEUCIEboskVThf4dvpZSrJLIXqnQL%2BKfnqS20ZPcmNsfo19YAiEAkVsCMmzg1%2FyFe5wm0Lhi8LsDd8qVW2S%2BhWljVvfE3mEq%2FwMIYhAAGgw2Mzc0MjMxODM4MDUiDJqlP0%2FceGu0iGj0syrcA9eoS6uv8Fm7eRIZdbPZKQ%2BkrHnsuzBuDa4d6LtFaEEl63WJ0EVO21RB5QHZWcLVt63oIIfjrPukTAC4mj3SQtDUGOTicuHKHDuWpvMsF8%2FewuIIakQTi%2Fu1h4b6eACbOcweSO9wguHp28VBBO%2FeueGePvDTgo2Rl9cpalOM%2BpP3lOx7GL366rImy6wu%2BI%2BvxnPOozVJuIpWaBzG95WdlnXfwx%2FEU%2Bk%2Bkw30FLauNP1Qt1%2F%2Bqf2i%2FDaYNWgfFi66DJ164IciLsXyMo0x3Y31x4vQ8RGdpB7SijCEhhR%2FxWi8pXNryYvlWhZPDjaAoz3OMZvfDkP%2F7fpPd4Dsyc0Q%2FsUVFKI38h%2BdVABKygdSkV%2Fizrjkj6z1Kr5tm5%2B3qlBXQrHDzkke5rVB3%2B6vTXVsk52OUsPkl5p72NmHeKPsvMmSbw5np7JLZqYYo8RBOYnNeRQOMp9nVMZxA0JkjCuCmcAOyVJjJnkp1ByePLt%2FyPU2DAob%2BtAYTYEDwaxOPZB%2FIg0lU%2BoS3llkFrEU53vK3n3DFHMf6Cn1K9f%2FKFGjMariRD9CMsEuWS7bIcXlJhpZIcrE0JeGET2deanoIMtv%2FWesT25sfihEtJisU4Hig%2BiBZP0PGBXmDaNVv%2Fq3MIeSnNcGOqUBPsQ8Cngi8fcz7HBnH9XEChLA1oLgzdA%2B0F%2FgTGACBEeep2DaFVPscpcLUXDvbYBgyZSKxhFvrULF28huVPmzFswUXWID%2B5ftLuK9oWV7%2B3DAqzLqeghdKtQK8Uu8fSF4vkyWKPXs%2BLrX3yuWcLU5C7pTgQy7nIMk9f5Vi9ZTDIxe4AHby6kan4PVs3a8MTYL%2BQNQ6E%2F8Hllb5fK8DVqaYdrfXy6y&X-Amz-Signature=eac5426ede25a9a63165f9d66a3ad888fdedc7f7c3c9e11112d55ad70c13be37&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) ## Git Under the Hood: Opening the "Black Box" We all know the commands: git add, git commit, git push. We type them, magic text scrolls by, and our code is saved. But have you ever stopped to ask: What is actually happening inside the engine? Understanding Git’s internals isn't just for trivia—it’s the secret to fixing merge conflicts and recovering "lost" code with total confidence. --- ## 1. The .git Folder: The Brain of the Operation If you run ls -a in your terminal, you’ll see a hidden folder named .git. This folder IS your repository. * If you delete your code but keep the .git folder, you can restore everything. - If you delete the .git folder, your history is gone forever. You're just left with simple text files. ### What’s inside? - HEAD: A file that tells Git where you currently are (which branch). - refs/: Pointers to your commits (branches and tags). - objects/: The database. This is where the real magic happens. ![Illustration accompanying: What’s inside?](https://prod-files-secure.s3.us-west-2.amazonaws.com/1daf8d7b-bd6a-4278-9213-f77aa4198ce5/47c3bafc-f356-47eb-bf7c-529ac8aafadb/935f906c-ddf9-4aae-bfbd-ab797e8d4145.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Am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FgTGACBEeep2DaFVPscpcLUXDvbYBgyZSKxhFvrULF28huVPmzFswUXWID%2B5ftLuK9oWV7%2B3DAqzLqeghdKtQK8Uu8fSF4vkyWKPXs%2BLrX3yuWcLU5C7pTgQy7nIMk9f5Vi9ZTDIxe4AHby6kan4PVs3a8MTYL%2BQNQ6E%2F8Hllb5fK8DVqaYdrfXy6y&X-Amz-Signature=c287154fdad0c91bb34cb9b32664d5501e17178ce17d95a49fc08edf7a54b266&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) --- ## 2. The Building Blocks: Blobs, Trees, and Commits Git is a "Key-Value Store." You give it content (Value), and it gives you a unique ID (Key). It uses three main objects: ### I. The Blob (Content) When you save a file, Git ignores the filename and focuses only on the content. It compresses that text and stores it as a "Blob." > Logic Check: If you have two different files with the exact same "Hello World" text, how many Blobs does Git create? (Answer: Just one! Git is efficient.) ### II. The Tree (Structure) Since Blobs don't have names, the Tree object acts like a directory listing. It maps filenames (like index.js) to their corresponding Blob hashes. ### III. The Commit (Snapshot) The Commit object ties it all together. It contains: 1. A pointer to the Tree. 1. Author and timestamp. 1. A pointer to the Parent Commit (the history link). --- ![Illustration accompanying: III. The Commit (Snapshot)](https://prod-files-secure.s3.us-west-2.amazonaws.com/1daf8d7b-bd6a-4278-9213-f77aa4198ce5/bea18c9a-40d2-431e-b87c-4e9c281f325f/1094da74-9fe7-499a-ae20-681238abcc2a.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB4665NW6QBMF%2F20261101%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20261101T133309Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEJr%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLXdlc3QtMiJHMEUCIEboskVThf4dvpZSrJLIXqnQL%2BKfnqS20ZPcmNsfo19YAiEAkVsCMmzg1%2F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0a44512505480e5e316b49a2&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) ## 3. The Lifecycle: From add to commit Let’s trace the journey of a single change: ### Step 1: git add . The moment you run this, Git immediately creates a Blob for your content and puts it in the objects/ folder. - Takeaway: git add saves your content to the database even before you commit! ### Step 2: git commit -m "Message" Git now captures the project structure. It creates a Tree to record your filenames and a Commit object to record the "Who, When, and Why." Finally, it moves your branch pointer to this new commit. ![Illustration accompanying: Step 2: git commit -m "Message"](https://prod-files-secure.s3.us-west-2.amazonaws.com/1daf8d7b-bd6a-4278-9213-f77aa4198ce5/e61fce54-77b4-45c4-9708-a9358cb3020c/d16b5851-4411-45c8-8f61-667934e82044.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB4665NW6QBMF%2F20261101%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20261101T133309Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEJr%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLXdlc3QtMiJHMEUCIEboskVThf4dvpZSrJLIXqnQL%2BKfnqS20ZPcmNsfo19YAiEAkVsCMmzg1%2FyFe5wm0Lhi8LsDd8qVW2S%2BhWljVvfE3mEq%2FwMIYhAAGgw2Mzc0MjMxODM4MDUiDJqlP0%2FceGu0iGj0syrcA9eoS6uv8Fm7eRIZdbPZKQ%2BkrHnsuzBuDa4d6LtFaEEl63WJ0EVO21RB5QHZWcLVt63oIIfjrPukTAC4mj3SQtDUGOTicuHKHDuWpvMsF8%2FewuIIakQTi%2Fu1h4b6eACbOcweSO9wguHp28VBBO%2FeueGePvDTgo2Rl9cpalOM%2BpP3lOx7GL366rImy6wu%2BI%2BvxnPOozVJuIpWaBzG95WdlnXfwx%2FEU%2Bk%2Bkw30FLauNP1Qt1%2F%2Bqf2i%2FDaYNWgfFi66DJ164IciLsXyMo0x3Y31x4vQ8RGdpB7SijCEhhR%2FxWi8pXNryYvlWhZPDjaAoz3OMZvfDkP%2F7fpPd4Dsyc0Q%2FsUVFKI38h%2BdVABKygdSkV%2Fizrjkj6z1Kr5tm5%2B3qlBXQrHDzkke5rVB3%2B6vTXVsk52OUsPkl5p72NmHeKPsvMmSbw5np7JLZqYYo8RBOYnNeRQOMp9nVMZxA0JkjCuCmcAOyVJjJnkp1ByePLt%2FyPU2DAob%2BtAYTYEDwaxOPZB%2FIg0lU%2BoS3llkFrEU53vK3n3DFHMf6Cn1K9f%2FKFGjMariRD9CMsEuWS7bIcXlJhpZIcrE0JeGET2deanoIMtv%2FWesT25sfihEtJisU4Hig%2BiBZP0PGBXmDaNVv%2Fq3MIeSnNcGOqUBPsQ8Cngi8fcz7HBnH9XEChLA1oLgzdA%2B0F%2FgTGACBEeep2DaFVPscpcLUXDvbYBgyZSKxhFvrULF28huVPmzFswUXWID%2B5ftLuK9oWV7%2B3DAqzLqeghdKtQK8Uu8fSF4vkyWKPXs%2BLrX3yuWcLU5C7pTgQy7nIMk9f5Vi9ZTDIxe4AHby6kan4PVs3a8MTYL%2BQNQ6E%2F8Hllb5fK8DVqaYdrfXy6y&X-Amz-Signature=eb7e2365d67455b670db1930ea1806ee57b1562f89208913dc005c99e41ff692&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) --- ## 4. Why the Long Hashes? (SHA-1) You’ve seen those long strings like e4a2b9.... These are Hashes. Because the hash is calculated from the exact content: - If you change one single space in your code, the Blob hash changes. - This causes the Tree hash to change. - Which causes the Commit hash to change. This makes Git immutable. You can’t secretly change old code without breaking the entire chain of hashes! --- ## Guided Logic: Building Your Mental Model Imagine you accidentally deleted a file from your folder, but you haven't committed yet. You know you ran git add five minutes ago. Think about the lifecycle: Since git add immediately creates a Blob in the .git/objects folder, is your work actually lost? (Hint: No! The content is already in the database; you just need to find its hash!) --- ## Conclusion Git isn't magic; it's a map of Content (Blobs), Names (Trees), and History (Commits). Next Step: Want to see the raw "brain" of your project? Run this command in your terminal: git cat-file -p HEAD It will show you the raw Commit object. You can then copy the Tree hash and run the command again to see your file structure!