Fundamentals 8 min read

Unlock the Secrets of the .git Directory: A Complete Guide to Git Internals

This article explains the hidden .git folder structure, detailing each file and directory such as HEAD, refs, objects, logs, config, index, and hooks, and shows how to read commit data and use stash with practical command examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Unlock the Secrets of the .git Directory: A Complete Guide to Git Internals

Every Git repository contains a hidden .git directory that stores all the internal data—branches, commit history, configuration, logs, and more.

HEAD (current branch)

The HEAD file records the name of the branch currently checked out. Its content looks like: ref: refs/heads/main or, for a development branch:

ref: refs/heads/dev

refs

Under .git/refs you will find three sub‑directories— heads, tags and remotes —that store the latest commit references for local branches, tags, and remote branches respectively.

|----refs
|  |----heads
|  |  |----main
|  |----tags
|  |----remotes
|  |  |----origin
|  |  |  |----main

.git/refs/heads

Each local branch has a file here containing the hash of its most recent commit. For example, the main branch file contains: 2ddc65b74a186f4332d7b218be0f18dd404c82ef This hash points to an object in .git/objects that holds the full commit details.

.git/refs/remotes

This directory mirrors the remote‑tracking branches, allowing Git to compare local and remote states.

.git/refs/tags

When you tag a release, a file is created here that records the hash of the commit the tag points to.

.git/refs/stash

If you use git stash, a stash entry appears here. Typical use‑cases include:

Temporarily switching to another branch without committing current changes, then restoring them with git stash pop or git stash apply.

Saving unfinished work while pulling updates that might cause conflicts, then re‑applying the saved changes.

Commit objects (.git/objects)

The refs entries ultimately point to objects stored in .git/objects. Each object file is named by splitting the 40‑character SHA‑1 hash: the first two characters form a directory, the remaining 38 characters form the file name.

For the example hash above, the object resides at:

.git/objects/2d/dc65b74a186f4332d7b218be0f18dd404c82ef

To view the commit content, use:

git cat-file -p 2ddc65b74a186f4332d7b218be0f18dd404c82ef

The output shows the tree hash, author, committer, and commit message. The tree hash can be inspected with:

git cat-file -p f8803d7d049edbbc981da732b855419d039f79ff

Blob objects (file contents) are displayed similarly with git cat-file -p.

Logs (.git/logs)

The .git/logs directory records every reference update. Each line contains the pre‑update hash, post‑update hash, author, timestamp, and commit message.

|----logs
|  |----HEAD
|  |----refs
|  |  |----heads
|  |  |  |----main
|  |  |----remotes
|  |  |  |----origin
|  |  |  |  |----main

config (configuration)

The repository‑specific configuration lives in .git/config. A typical file includes core settings, remote definitions, and branch merge information:

[core]
 repositoryformatversion = 0
 filemode = true
 bare = false
 logallrefupdates = true
 ignorecase = true
 precomposeunicode = true
[remote "origin"]
 url = https://github.com/xxxx/xxxx-blog.git
 fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
 remote = origin
 merge = refs/heads/main

index (staging area)

The .git/index file represents the staging area. When you run git add, Git records a snapshot of the added files in this index. A subsequent git commit writes the index contents to a new commit object.

hooks

Hooks are executable scripts that run at specific points in Git’s workflow, such as pre-commit, post-commit, or pre-push. They allow custom actions—like code linting or deployment—to be triggered automatically.

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.

GithooksVersion ControlCONFIGcommitgit internalsdotgit
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.