DEV Community

Hiral
Hiral

Posted on

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

In my previous blog, I talked about why version control is important.
Now let’s dive into the most widely used version control system — Git.

We’ll start with a basic command:

git init

This command initializes a Git repository by creating a hidden directory called .git.
This folder is responsible for tracking every change in your project.

Structure of .git folder

  1. HEAD
    It tells which branch or commit you are on
    Eg. ref: refs/heads/main
    It means your current branch is main and head points to main

  2. config
    It stores repo related configuration
    like remote urls, branch tracking info

  3. index
    index file represents the staging area
    e.g if you run " git add feature.txt"

    • git reads the file
    • create blob object
    • records it reference in the index
  4. description
    Human readable description of repository

  5. objects/
    It is heart of git. All data is stored here as objects

    • Blob -> File content
    • Tree structure -> directory structure
    • commit - > metadata + pointer to tree (Sanpshot) So Relationship is commits -> tree structure -> blob
  6. ref
    It stores reference(pointer) to commits

    • ref/heads -> local branches and each file contains hash commits
    • ref/mains ->remote branches
    • ref/tags -> release point
  7. hooks
    Hooks are script that automatically on git events.
    like precommit ,postcommit

    • used for code commits
    • linting
    • prevent bad commits
  8. info
    stores local metadata

  9. logs
    git logs every change from head and branches over here

How to Git track changes?

Git does not store line by line changes instead stores whole snapshot
Files that not changes uses the existing snap shot.This makes git fast and efficient.

** What happens when run git add ?**

when you run git add text.txt

  • Git reads the file context
  • creates a blob object
  • stores in objects/
  • Records it in the index file i.e staging area

what happens run git commit?

  • reads staging area
  • creates a tree object
  • creates a commit object
  • Updates the branch reference

Git flow
Working area -> staging area(index) -> commit(snapshot)

Now that you understand what’s inside the .git folder, I hope you feel more confident using Git instead of just memorizing commands

Top comments (0)