Skip to main content

Command Palette

Search for a command to run...

How Git Works Internally: Build a Mental Model Instead of Memorizing Commands

Published
3 min read
How Git Works Internally: Build a Mental Model Instead of Memorizing Commands
  1. What is the .git Folder and Why It Exists

When you run:

git init

Git creates a .git directory.
This folder is the entire Git repository. Everything Git knows about your project lives here.

Your working directory = files you see
.git directory = Git’s brain

If you delete .git, your project becomes a normal folder.

Basic structure:

.git/
├── objects/
├── refs/
├── HEAD
├── index
├── config

Meaning:

  • objects/ → where all data is stored (commits, files, trees)

  • refs/ → branch and tag pointers

  • HEAD → tells you which branch you’re on

  • index → staging area

  • config → repo-level configuration

]


  1. Git Objects: Blob, Tree, Commit

Everything in Git is an object. There are only three main ones you need:

ObjectRepresents
BlobFile content
TreeDirectory structure
CommitSnapshot + metadata

Git does not store “files”.
It stores content.

Example mental model:

Commit
  ↓
 Tree
  ↓
Blobs (file contents)

Blob:

  • Stores raw file data

  • No filename

  • No path

Tree:

  • Maps filenames → blobs

  • Represents a folder

Commit:

  • Points to a tree

  • Has author, message, timestamp

  • Points to parent commit(s)


  1. How Git Tracks Changes

Git does not track “changes”.
It tracks snapshots.

Every commit is a full snapshot of your project.
But Git is smart:

  • If a file didn’t change, its blob is reused.

  • Only new content creates new objects.

So Git is efficient without using diffs internally.

Mental shift:

Git stores versions of your project, not versions of lines.


  1. What Happens During git add

When you run:

git add file.txt

Internally:

  1. Git reads the file

  2. Creates a blob object

  3. Hashes the content

  4. Stores it in .git/objects

  5. Updates the index (staging area)

Nothing is committed yet.

Working Directory → Staging Area


  1. What Happens During git commit

When you run:

git commit -m "message"

Internally:

  1. Git reads the index

  2. Builds a tree object

  3. Creates a commit object

  4. Stores it in .git/objects

  5. Moves branch pointer to this commit

Staging Area → Commit → Branch updated


  1. How Git Uses Hashes

Git uses SHA-1 (or SHA-256 in newer versions).

Each object:

hash = SHA(content + metadata)

This gives:

  • Integrity: content cannot change without hash changing

  • Deduplication: same content → same hash

  • Security: corruption is detectable

That’s why:

.git/objects/ab/cdef...

Folders are split using hash prefixes.

Mental model:

Git doesn’t name files. It names content.


  1. Building the Real Mental Model

Forget commands. Think in layers:

Working Directory
        ↓
     Staging Area (index)Commit (objects)
        ↓
    Branch pointer

Commands just move data between these layers.

CommandMeaning
git addSave content into Git
git commitCreate a snapshot
git checkoutMove pointers
git resetMove history references

  1. Why This Model Makes You Strong in Git

Once you understand this:

  • Merge conflicts make sense

  • Rebase is not scary

  • Detached HEAD is logical

  • You can fix mistakes confidently

You stop guessing and start controlling Git.