Inside the .git Folder
When you open the back of a wall clock, the mystery disappears. There is a battery, a small motor and a few gears, and you can see how each one moves the next.
The .git folder is the back of the clock. Everything Git knows about your project is in there, stored as ordinary files and folders that you can list and read. There is no hidden database and no server.
You don't need this lesson to use Git every day. But ten minutes inside .git removes most of the fear. When you know that a branch is a tiny text file and a commit is a file in a folder, Git stops feeling like magic that might break.
A New Repository
Navika creates a repository and lists what git init put inside .git. The -F option of ls adds a slash after the names of folders:
mkdir fee-tracker
cd fee-tracker
git init --quiet
ls -F .gitHEAD
config
description
hooks/
info/
objects/
refs/Seven items, and at this point they are almost empty:
| Item | What it is for |
|---|---|
HEAD | Says where you are now: which branch you are on |
config | The local settings of this repository |
objects/ | Where Git stores all content: every commit and every version of every file |
refs/ | Where Git stores branches and tags |
hooks/ | Example scripts that Git can run automatically at certain moments |
info/ | A place for extra settings that are not shared with others |
description | A text file used only by some web interfaces; you can ignore it |
The first four matter most. This lesson looks at HEAD, objects and refs. The config file is the local level you met in "Git Config: Setting Up Git".
HEAD: Where You Are
HEAD is a small text file:
cat .git/HEADref: refs/heads/mainIt holds one line, and the line is a signpost: "you are on the branch stored at refs/heads/main". When git status prints On branch main, this file is where it reads the name.
Here is something odd. The repository has no commits yet, so the branch main doesn't exist as a file:
ls .git/refs/heads
find .git/objects -type f Both commands print nothing. The heads folder is empty and there is not one stored object. HEAD points to a branch that will be created by the first commit. That is exactly what git status means by "No commits yet".
git add Stores Content
Now she creates a file and stages it. The find command lists every file in the objects folder:
echo "Fee Tracker" > README.md
git add README.md
find .git/objects -type f.git/objects/d5/924b0d02d9ef4f29fef5369a7ddcaf82c830c7One object appeared, before any commit. git add did two things: it stored the content of README.md in objects, and it wrote a line about it in the staging area.
Look at the object's path. The folder is named d5 and the file 924b0d02.... Put together, they are the 40-character ID of the content: d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7. Git uses the first two characters as a folder name so that no single folder ends up with millions of files.
If you read "How Git Works: Snapshots, Not Differences", you have seen this ID before. A file that contains Fee Tracker gets d5924b0 in every repository on every computer.
A Commit Adds Two More Objects
git commit -m "Add project README"[main (root-commit) ae1a9bb] Add project README
1 file changed, 1 insertion(+)
create mode 100644 README.mdfind .git/objects -type f | sort.git/objects/ae/1a9bbbe6d947ab028ffb6fc2ec60d4326317fc
.git/objects/b5/6cb63ce4c1986636394834fbee61651b8b58d1
.git/objects/d5/924b0d02d9ef4f29fef5369a7ddcaf82c830c7There are three objects now. (| sort puts the list in alphabetical order.) git cat-file -t prints the type of an object:
git cat-file -t d5924b0
git cat-file -t b56cb63
git cat-file -t ae1a9bbblob
tree
commit| Object | Type | What it holds |
|---|---|---|
d5924b0 | blob | The content of README.md |
b56cb63 | tree | The snapshot: a list that says "README.md has the content d5924b0" |
ae1a9bb | commit | The author, the date, the message and the ID of the tree |
One commit with one file is three small files on disk. The object files are compressed, so cat shows unreadable characters. git cat-file -p <id> prints an object in readable form.
refs: A Branch Is a Small File
The commit also created the branch. The file that HEAD pointed to now exists:
cat .git/refs/heads/mainae1a9bbbe6d947ab028ffb6fc2ec60d4326317fcThat is the whole branch: a file that contains the ID of one commit. When you make another commit, Git writes the new commit's ID into this file. This is what "the branch moves forward" means, and it is why creating a branch in Git is instant, however large the project is.
Now the chain is complete, and you can follow it the way Git does:
HEADsays: you are onrefs/heads/main.refs/heads/mainsays: the newest commit isae1a9bb.- The commit
ae1a9bbsays: my snapshot is the treeb56cb63. - The tree says:
README.mdhas the content in blobd5924b0.
What Else Appeared
List the folder again after the first commit:
ls -F .gitCOMMIT_EDITMSG
HEAD
config
description
hooks/
index
info/
logs/
objects/
refs/Three new items:
| Item | What it is for |
|---|---|
index | The staging area. It was created by your first git add. |
logs/ | A diary of every position HEAD and each branch has had. Git uses it to recover lost work, as you will see in the lessons on the reflog. |
COMMIT_EDITMSG | The message of your most recent commit |
So the three areas from the lesson "Working Tree, Staging Area and Repository" are all visible on disk. The working tree is the files next to .git. The staging area is the file .git/index. The repository is .git/objects and .git/refs.
Common Mistakes
Deleting .git to "start fresh". This deletes every commit. If you only want to undo recent work, Git has safe commands for that, which the course covers in the lessons on undoing changes.
Editing files in .git by hand. A typing mistake in HEAD or in a branch file can leave the repository unusable. Use git switch, git branch and git config to change what these files hold.
Copying a project without its .git folder. The copy has the files and no history. Hidden folders are easy to miss when you drag files in a file manager. Copy the whole project folder, or better, use git clone.
Putting the .git folder in a synced cloud drive. Tools such as Dropbox and OneDrive copy files one at a time, and can catch .git halfway through a change and damage it. Share repositories with git push and git pull.
Thinking .git is uploaded as it is. When you push, Git sends commits and branch positions. Your local settings, hooks, staging area and logs stay on your computer.
Under the Hood
A few details for the curious:
- Objects are compressed with zlib and never change. An object's name is the hash of its content, so changing the content would give it a different name. Git only ever adds objects.
- Loose objects and packfiles. The one-file-per-object layout you saw is called loose objects. As a repository grows, Git gathers them into a few large packfiles in
.git/objects/packto save space. The objects are the same and the commands behave the same. - Branches may be packed too. In a repository with many branches, Git can store them together in one file named
packed-refs, andrefs/headsmay then look empty. A newer storage format for branches, called reftable, is planned to become the default in Git 3.0. The commands you type stay the same.git rev-parse mainprints the commit a branch points to, whichever format is in use. - The index is a binary file, so
cat .git/indexis not readable. Usegit ls-files --stageto see what it holds.
Interview Corner
What is stored in the .git folder?
The whole repository: the objects (commits, trees and blobs) in objects/, the references (branches and tags) in refs/, HEAD, which records the current branch, the index (the staging area), the repository's local config, plus logs and sample hooks. The working tree, which is the files you edit, is outside it.
What is HEAD?
HEAD is a reference to the commit you are currently working from. Normally it is a symbolic reference to a branch: the file .git/HEAD contains ref: refs/heads/main, and that branch in turn holds a commit ID. When a new commit is made, the branch that HEAD points to moves to it.
What is a branch, physically?
A reference: a name that holds the 40-character ID of a commit. In the simplest storage format, it is a small text file under .git/refs/heads/. Creating a branch writes one such file, which is why it is fast and takes almost no space.
When is a blob created: at git add or at git commit?
At git add. Staging writes the file's content into the object store as a blob and records its ID in the index. git commit then creates the tree and the commit object from the index.
Why does Git store objects in folders named with two characters?
The first two characters of the object's ID become the folder name and the remaining 38 the file name. This spreads objects over 256 folders, so no single folder holds a huge number of files, which keeps file system lookups fast.
Practice
Challenge: watch the branch move. Continue in the same repository. Add the line Tracks school fee payments. to README.md, stage it, and commit it with the message Describe what the app does. Then print the branch file and the history.
What does Git print? Before you open the solution, decide what .git/refs/heads/main will contain after the commit: the old ID ae1a9bb..., or something else.
Show solution
echo "Tracks school fee payments." >> README.md
git add README.md
git commit --quiet -m "Describe what the app does"
cat .git/refs/heads/main
git log --oneline51655eea1a8d3e00e58416fb9164f691f65e2ac6
51655ee (HEAD -> main) Describe what the app does
ae1a9bb Add project READMEThe branch file now holds the ID of the new commit, 51655ee. Git replaced one line in one small file, and the branch "moved". The old commit is still in objects, and the new commit points back to it as its parent.
Count the objects. How many files are in .git/objects now?
find .git/objects -type f | sort.git/objects/22/3bab126d5f9484c2d82349beacc6fe37c16951
.git/objects/51/655eea1a8d3e00e58416fb9164f691f65e2ac6
.git/objects/ae/1a9bbbe6d947ab028ffb6fc2ec60d4326317fc
.git/objects/b5/6cb63ce4c1986636394834fbee61651b8b58d1
.git/objects/b5/b7c2b6fdfc350d96e7e23b91f859824cdc574a
.git/objects/d5/924b0d02d9ef4f29fef5369a7ddcaf82c830c7Six: the three from the first commit, plus a new blob (the two-line README.md), a new tree and a new commit. Nothing was changed or removed.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
ls -F .git | List what is inside the .git folder |
cat .git/HEAD | Show which branch HEAD points to |
cat .git/refs/heads/main | Show the commit ID that the branch main holds |
find .git/objects -type f | List every stored object file |
git cat-file -t <id> | Print the type of an object: blob, tree or commit |
git cat-file -p <id> | Print an object in readable form |
Was this tutorial helpful?