Skip to main content

Command Palette

Search for a command to run...

How git works internally

Published
3 min read
How git works internally
S

Hi, I am a web developer, always curious and exploring new tech

A thought that comes to many developers is: why do we need to know Git internally?

Knowing Git internally helps us understand it more clearly because it strengthens our foundation.
When a Git-related problem arises, this deeper understanding allows us to debug issues more easily.

Now, to start with how Git works internally, let’s begin from the very start.

What happens when we run git init?

Git creates a hidden folder named .git.

This folder is the entire Git repository.

If you delete .git Your project is no longer a Git repository.

What does the .git folder contain? Let’s break down each file and folder you see and understand its real purpose.

HEAD — Current Position Pointer

  • Points to the current branch

  • Initially references the default branch

config — Repository Configuration

Stores repo-specific settings such as:

  • Default branch name

  • Remote URLs (added later after we push code to GitHub or any other cloud git repository provider)

  • Merge and fetch behavior

description — GitWeb Metadata

  • Used by GitWeb (mostly legacy)

  • Safe to ignore for modern workflows

hooks/ — Automation Scripts (Empty by Default)

Contains sample hook scripts, like:

  • pre-commit.sample

  • commit-msg.sample

Hooks allow automation:

  • Run tests before commit

  • Block commits with bad messages

They are disabled until renamed and made executable.

info/ — Local Ignore Rules

  • Works like .gitignore

  • Applies only to this local repo

  • Not committed or shared

objects/ — Object Database (Empty Initially)

This is where Git stores:

  • File contents (blobs)

  • Directories (trees)

  • Commits

  • Tags

At git init time:

  • No objects exist yet

  • Directory is mostly empty

  • After adding some commit a new hash object will be stored

refs/ — References (No Commits Yet)

  • heads/ → branch pointers

  • tags/ → tag pointers

At this stage:

  • Branch files do not exist yet

  • They are created only after the first commit

Summary: What Happens When You Run git init

  • Running git init creates a hidden .git directory, which is the entire Git repository.

  • This directory contains only the basic structure, not any commits or history.

Created immediately:

  • HEAD → points to the default branch (no commits yet)

  • config → repository-specific Git settings

  • description → legacy GitWeb metadata

  • hooks/ → sample automation scripts (disabled by default)

  • info/exclude → local ignore rules

  • objects/ → empty object database

  • refs/heads and refs/tags → placeholders for branches and tags

Not created at this stage:

  • No commits

  • No branch files

  • No staging area (index)

  • No logs or history

In short, git init prepares the foundation.
Git starts tracking code only after your first git add and git commit.