Sub topic 2
A tour of the .git directory
This chapter assumes you can use git init, add, commit, log and branch, and that you are comfortable in a Unix-style shell. You will open the .git directory of a fresh repository and learn what each part is for. Later chapters work with the object database in detail.
Why look inside .git
Every Git repository keeps its entire state in one directory, .git, at the top of the working tree. Your project files are only a checkout. The history, branches, tags and configuration all live inside .git. If you copy that directory, you copy the repository.
Create a repository and list it
Run the first example from the plan:
git init plumbing-demo
cd plumbing-demo
ls .git
find .git/objects -type fThe exact listing varies by Git version and platform. A new repository usually shows these entries:
| Entry | What it holds |
|---|---|
| `HEAD` | A text file naming the current branch, or a commit. |
| `config` | Repository-level settings, such as `user.name`. |
| `description` | Used only by the legacy GitWeb tool. |
| `hooks/` | Sample scripts that run on events such as commit. |
| `info/` | Extra data such as `info/exclude`, a local ignore list. |
| `objects/` | The object database, which stores your content. |
| `refs/` | Names (branches and tags) that point at commits. |
The find command prints nothing. A new repository has no object files yet, because you have not stored anything.
The three parts that matter most
Most of what you will explore sits in three places.
objects/
This is the content-addressable object database. Git stores each blob, tree, commit and annotated tag here, under a name derived from its content. A new repository contains only two empty subdirectories, info and pack. Loose objects go in subdirectories named after the first two characters of their hash. Packed objects go in pack/. You will see both forms in the chapter on packfiles.
refs/
This directory holds the names that point at commits. Branches live under refs/heads/ and tags under refs/tags/. Each ref is a small file containing one commit hash. Until you make a commit, refs/heads/ is empty, which is why a new repository has no branch yet in the usual sense.
HEAD
HEAD records where you are. Print it:
cat .git/HEADOn a new repository this prints a line such as ref: refs/heads/main. The name may be master on older setups, depending on init.defaultBranch. This is a symbolic reference. It does not hold a hash. It holds the path of another ref. That ref does not exist yet, so the branch is "unborn". It appears when the first commit is created.
The index, which appears later
git init does not create the index file. It appears when you first stage something. It holds the staging area, covered in its own chapter.
Common errors
- Looking in the wrong place.
.gitis in the repository root, not in subdirectories. Rungit rev-parse --git-dirfrom anywhere inside the repository to print its path. - Editing files by hand. Writing to
HEADorrefs/can break the repository. Use plumbing commands such asgit update-refandgit symbolic-ref, which you will meet shortly. - Expecting objects after
git init. The object directory is empty until something is stored. - Assuming
.gitis always a directory. In a linked worktree or submodule,.gitis a file that points to the real directory.
Exercise
You run git init demo and then cat demo/.git/HEAD, which prints ref: refs/heads/main. You run git log and Git reports that the branch has no commits. Which file or directory is missing, and why?
Show answer
The file .git/refs/heads/main does not exist. HEAD is a symbolic reference naming that path, but no commit has been created, so no branch file has been written. It is created when the first commit is made.