DEV Community

Cover image for Inside Git
Swadesh Chatterjee
Swadesh Chatterjee

Posted on

Inside Git

Introduction

Most developers learn Git through commands like git commit, git push, or git merge, but they often rely on memorization or online guides to get things done. Understanding Git internal design how it stores data, tracks history, and ensures integrity can turn confusing errors into predictable outcomes. Instead of just memorizing commands, you can reason about Git.

What Happens When You Run git init

Running git init does one main thing: it creates a hidden .git directory in your project root. This folder is Git’s database and control center—everything Git needs to manage your project is stored here. Without it, you’re just working with plain files.

Understanding the .git Folder

The .git directory contains several key components:

  • HEAD – A pointer to the current branch or commit you’re on.
  • objects/ – Git’s object database, storing all blobs, trees, commits, and tags.
  • refs/ – References to commits: branches (under refs/heads), tags (refs/tags), and more.
  • config – Project-specific Git configuration.
  • index – The staging area, a binary file that tracks what will go into the next commit. I

What Happens During git add

When you run git add file.txt:

  1. Git compresses the file’s content and creates a blob object.
  2. The blob’s hash (e.g., a1b2c3...) is computed from its content.
  3. The blob is written to .git/objects/.
  4. The file’s hash and path are recorded in the index (staging area).

The staging area is essentially a list of what will be part of the next tree object.

What Happens During git commit

When you commit:

  1. Git creates a tree object from the current index, representing the project structure.
  2. A commit object is created containing:
    • The tree’s hash
    • Parent commit hash(es)
    • Author, committer, timestamp
    • Commit message
  3. The commit object is stored in .git/objects/.
  4. The current branch reference (e.g., refs/heads/main) is updated to point to the new commit hash.

Im

Top comments (0)