Sub topic 2
A tour of the .git directory
This chapter assumes you can use Git day to day (init, add, commit, log, branch) and are comfortable in a Unix-style shell. Everything Git knows about a project lives in one directory, .git. Here you will look at what each entry in it is for, watch it change as you run ordinary commands, and learn which parts you can safely read and which you should leave alone.
Why look inside .git
Git has no server-side database or hidden service for a local repository. A repository is a working tree plus a directory of files. If you know the layout, you can answer questions like these without guessing:
- Where does a branch name get stored?
- Why did my commit not appear after I interrupted a command?
- What is Git actually reading when it says "detached HEAD"?
The later chapters of this tutorial go deep on the object database. This chapter only maps the territory.
Start with an empty repository
Create a repository and list everything inside .git. The -b main option sets the initial branch name explicitly (it needs Git 2.28 or later), so your output matches the examples whatever your init.defaultBranch setting is.
git init -b main dir-tour
cd dir-tour
ls -A .gitA fresh repository typically contains these entries:
HEAD
config
description
hooks
info
objects
refsSome Git versions or templates also create an empty branches directory. It is a leftover from an old feature and Git does not use it. Notice what is missing: there is no index file yet, and no logs directory. Git creates those when it first needs them.
The entries, one by one
| Entry | Kind | Purpose |
|---|---|---|
| `HEAD` | file | Says which branch (or commit) is currently checked out |
| `config` | file | Repository-local configuration |
| `description` | file | Text used only by GitWeb; ignore it |
| `hooks/` | directory | Scripts Git runs at certain points, such as before a commit |
| `info/` | directory | Extra repository-local settings, such as `info/exclude` |
| `objects/` | directory | The object database: file contents, trees, commits, tags |
| `refs/` | directory | Named pointers to commits: branches and tags |
| `index` | file | The staging area (appears after the first `git add`) |
| `logs/` | directory | The reflog, a history of where refs and HEAD pointed |
| `COMMIT_EDITMSG` | file | The last commit message you edited |
| `packed-refs` | file | Refs compacted into one file, created by `git pack-refs` or `git gc` |
Two of these, objects/ and refs/, hold the actual repository data. Most of the other entries support them.
HEAD: a file with one line
HEAD is a plain text file. Read it:
cat .git/HEADref: refs/heads/mainThis is a symbolic reference. It means "I am on the branch stored at refs/heads/main." That path does not exist yet, because the branch has no commits. Git calls this an unborn branch. When you check out a commit directly (a detached HEAD), the file holds a raw 40-character commit hash instead of a ref: line.
config: settings that apply here
cat .git/configYou will see a [core] section with entries such as repositoryformatversion, filemode and bare. This file is what git config --local reads and writes. Settings are layered: the system file, your global ~/.gitconfig and this file are all read, and the more local file wins. Remotes you add with git remote add and branch tracking settings also end up here.
refs: branches and tags as files
List the refs directory:
find .git/refs.git/refs
.git/refs/heads
.git/refs/tagsrefs/heads/ holds local branches and refs/tags/ holds tags. Remote-tracking branches go in refs/remotes/, which appears once you fetch from a remote. Each ref is a file whose content is a commit hash. The next chapters on objects and refs return to this in detail.
objects: where the data goes
find .git/objects.git/objects
.git/objects/info
.git/objects/packBoth subdirectories are empty. pack/ will hold packfiles, and info/ holds auxiliary lists. Loose objects, which are single files for single objects, will appear in directories named after hash prefixes.
Watching .git change
The best way to understand the layout is to run ordinary porcelain commands and see what they write. The -c options below supply an identity for this one command so the commit works even if you have no global user settings.
echo 'hi' > a.txt
git add a.txt
ls -A .git
find .git/objects -type fAfter git add, an index file exists and objects/ contains one new file. That file is the stored content of a.txt. The path looks like .git/objects/45/b983be36b73c0788dc9cbcb76cbb80fc7bb057: the first two hex digits of the hash become a directory name and the remaining digits the file name.
Now commit and look again:
git -c user.name='Demo' -c user.email='[email protected]' commit -m 'add a.txt'
ls -A .git
cat .git/refs/heads/main
git rev-parse HEAD
find .git/objects -type fSeveral things changed:
refs/heads/mainnow exists and contains a commit hash.git rev-parse HEADprints the same hash because HEAD namesrefs/heads/main.objects/now holds three files: the content ofa.txt, a tree for the directory, and the commit.COMMIT_EDITMSGandlogs/appeared.
Look at the reflog files:
cat .git/logs/HEAD
cat .git/logs/refs/heads/mainEach line records the old hash, the new hash, who did it, a timestamp, and a short reason such as commit (initial): add a.txt. This is what git reflog shows. Reflogs are local only and are never pushed.
Hooks
ls .git/hooksNew repositories get a set of files ending in .sample, such as pre-commit.sample. Git ignores them. To enable a hook, create an executable file with the exact hook name and no extension:
printf '#!/bin/sh\necho "about to commit"\n' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commitHooks live inside .git, so they are not versioned and not copied by git clone. Teams that want shared hooks usually keep them in the working tree and point core.hooksPath at that directory.
Edge cases and gotchas
.git is sometimes a file
In a linked worktree (made with git worktree add) or a submodule, .git is a text file with one line like gitdir: /path/to/real/gitdir. The real directory holds that worktree's own HEAD and index, while the object database and branch refs are shared with the main repository. Use these commands instead of assuming a path:
git rev-parse --git-dir
git rev-parse --git-common-dirBare repositories
A bare repository, as created by git init --bare, has no working tree. The contents that would normally sit inside .git (HEAD, objects/, refs/ and so on) sit directly in the repository directory. Servers use this layout.
packed-refs
When there are many refs, Git can store them in the single packed-refs file. A loose file in refs/heads/ takes precedence over an entry in packed-refs. So if a branch is missing from refs/heads/, it may be in packed-refs. Use git for-each-ref or git show-ref to list refs without caring where they are stored. Recent Git versions also support an alternative reftable storage format, which replaces these files, so scripts that read .git/refs directly can break there.
Editing files by hand
Reading .git is safe. Writing to it is not, except for a few files like config, info/exclude and hooks. The index is a binary file, and object files are compressed and are named after a hash of their content. If you change one by hand, Git will report corruption. Use plumbing commands to make changes, as the later chapters show.
Exercises
Exercise
In a repository on branch main with at least one commit, cat .git/HEAD prints ref: refs/heads/main. What command prints the commit hash that HEAD resolves to, and which file in .git holds the same hash?
Show answer
git rev-parse HEAD prints it. The same hash is stored in .git/refs/heads/main, unless the ref has been packed, in which case it is in .git/packed-refs.
Exercise
You run git init in a new directory and git add file.txt. Name one entry in .git that did not exist before the git add, and one place where Git stored new data.
Show answer
The index file is created by the first git add. The content of file.txt is stored as a new loose object under .git/objects/, in a directory named after the first two hex digits of its hash.
Exercise
You find that .git in a project is a file, not a directory. What is it likely to be, and how can you find the real Git directory?
Show answer
It is probably a linked worktree or a submodule, and the file contains a gitdir: line pointing to the real directory. Run git rev-parse --git-dir to print that location.