Fundamentals 22 min read

What Really Happens Inside Git? Exploring Init, Add, Commit, and Object Internals

This article demystifies Git by explaining the internal structures created by commands like git init, git add, and git commit, detailing the .git directory layout, object types, SHA‑1 hashing, the index, branches, HEAD pointers, and how remote repositories are linked and synchronized.

Liangxu Linux
Liangxu Linux
Liangxu Linux
What Really Happens Inside Git? Exploring Init, Add, Commit, and Object Internals

Understanding Git Init

Running mkdir git-demo && cd git-demo && git init creates a .git directory containing essential files such as HEAD, config, description, hooks, info, objects, and refs. The .git/config file stores repository‑specific settings, while .git/HEAD points to the current branch (e.g., ref: refs/heads/master).

Git Add and the Blob Object

When git add hello.txt is executed, Git creates a blob object that stores only the file’s content and type, not its name. The blob’s SHA‑1 hash (e.g., 8d0e41...) is derived from the string blob 10\0hello git. The object is saved under .git/objects/8d/0e41... and can be inspected with git cat-file -t and git cat-file -p.

# view blob type and content
$ git cat-file -t 8d0e41
blob
$ git cat-file -p 8d0e41
hello git

Working Tree, Index, and File Status

The working tree holds the actual files, while the index (stored in .git/index) records which file versions are staged. git status compares the working tree with the index to show untracked or modified files. Commands like git ls-files -s display the index’s staged entries.

Commit Objects and Tree Structure

Executing git commit -m "msg" creates a commit object that references a tree object representing the snapshot of the project at that point. The commit stores metadata (author, message, parent commit) and points to the tree, which in turn references blobs and sub‑trees. The new commit becomes the tip of the current branch, and .git/refs/heads/master is updated accordingly.

# inspect commit and tree
$ git cat-file -t 6e4a70   # commit
commit
$ git cat-file -p 6e4a70   # commit content
...metadata...
$ git cat-file -t 64d6ef5 # tree
$ git cat-file -p 64d6ef5 # tree content

Branches, HEAD, and Detached States

A branch is simply a named pointer to a commit (e.g., refs/heads/master). The special HEAD file always points to the current branch’s tip; when you checkout another branch, HEAD is updated. Checking out a specific commit creates a detached HEAD state, where HEAD points directly to that commit.

Remote Repositories

Linking a local repository to a remote (e.g., git remote add origin [email protected]:escapelife/git-demo.git) adds a [remote "origin"] section to .git/config. Pushing with git push -u origin master transfers objects to the remote server, creates remote‑tracking branches under refs/remotes/origin/, and updates local logs.

Cleaning Up and Garbage Collection

Deleted branches leave behind unreachable objects (garbage). Git’s garbage collection (e.g., git gc) eventually prunes these objects, reclaiming space.

Additional Tools

Python can decompress object files:

import zlib
contents = open('0e41234f24b6da002d962a26c2495ea16a425f', 'rb').read()
print(zlib.decompress(contents))

The article also covers diffing ( git diff, git diff --cached), reflog for recovering lost commits, and the role of .git/info/exclude for local ignore patterns.

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.

BranchcommitRemote Repositorygit internals
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.