Fundamentals 23 min read

What Really Happens Inside Git? Uncover the Secrets of init, add, commit, and branches

This article demystifies Git by explaining how the init command creates the .git directory structure, how objects, blobs, and SHA‑1 hashes store file contents, the role of the index, the mechanics of add, commit, branch, HEAD, remote linking, push, reflog, and diff operations, all illustrated with command‑line examples and diagrams.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
What Really Happens Inside Git? Uncover the Secrets of init, add, commit, and branches

Understanding Git Internals

In simple terms, Git is a distributed version‑control system that stores data differently from other VCS tools. Grasping its core concepts—how it initializes a repository, manages objects, and tracks file states—makes using Git intuitive and powerful.

git init and the .git Directory

Running git init creates a .git folder containing the metadata needed for version control. The structure includes HEAD , config , description , hooks , info , objects , and refs .

# left side execution
$ mkdir git-demo
$ cd git-demo && git init
$ rm -rf .git/hooks/*.sample

# right side execution
$ watch -n 1 -d find .

The resulting directory tree looks like:

➜ tree .git
.git
├── HEAD
├── config
├── description
├── hooks
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

.git/config

The local configuration file stores repository‑specific settings. You can set user information with:

git config user.name "demo"
git config user.email "[email protected]"

.git/objects – Storing File Contents

Git stores file data as objects of three types: blob (file content), tree (directory listings), and commit (snapshot metadata). Objects are identified by a SHA‑1 hash calculated from the object type, size, a null byte, and the content.

# create a file and add it
$ echo "hello git" > hello.txt
$ git add hello.txt
$ git cat-file -t 8d0e41   # shows "blob"
$ git cat-file -p 8d0e41   # shows file content
$ git cat-file -s 8d0e41   # shows size (10 bytes)

# assemble the raw object
blob 10\0hello git

Git compresses objects; small files may appear larger after compression, but larger files compress efficiently.

Index (Staging Area)

The index file ( .git/index ) records the filenames and the SHA‑1 of the corresponding blob objects. While its raw content looks like binary garbage, commands such as git ls-files -s reveal the staged files and their object IDs.

# stage a new file
$ echo "file1" > file1.txt
$ git add file1.txt
$ git ls-files -s   # list staged files with mode, SHA‑1, and path

git add Workflow

When you run git add, Git creates a blob object for the file content (without storing the filename) and updates the index. Adding identical content creates no new blob because Git deduplicates based on the SHA‑1 hash.

git commit Mechanics

A commit records a snapshot of the entire project tree. Git creates a tree object that references blobs (files) and sub‑trees (directories), then a commit object that points to that tree, includes parent commit IDs, author information, and a message.

# commit changes
$ git commit -m "1st commit"
$ git cat-file -t <commit-id>   # shows "commit"
$ git cat-file -p <commit-id>   # displays tree hash, parent, author, message

The HEAD file always points to the current branch’s latest commit. Switching branches updates HEAD accordingly.

Branches and HEAD

Branches are simply named pointers to commit objects. The special HEAD file points to the current branch name (e.g., ref: refs/heads/master) or directly to a commit in a detached state.

# list branches
$ git branch
# create a new branch
$ git branch dev
# switch to it
$ git checkout dev
# HEAD now points to dev

Deleting a branch removes only the pointer; the underlying commits remain until garbage‑collected.

Remote Repositories

Link a local repository to a remote server with:

git remote add origin [email protected]:escapelife/git-demo.git

The .git/config file then contains a [remote "origin"] section. Pushing sends objects to the remote, creating corresponding refs under refs/remotes/origin .

# push local master to remote
$ git push -u origin master

Reflog and Recovering Lost Commits

Even after deleting a branch, its commits are still reachable via git reflog, which records all reference updates. You can checkout a lost commit and create a new branch from it:

git reflog
git checkout <commit-id>
git checkout -b recovered

Diff Operations

Git compares the working tree, index, and HEAD to produce diffs. Common commands:

git diff          # unstaged changes
git diff --cached   # staged changes
git diff HEAD     # changes since last commit

Summary

Understanding Git’s internal objects, the index, branch pointers, and remote interactions reveals why the system is fast, reliable, and flexible for collaborative development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Version ControlRepositoryBranchcommit
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.