Inside the .git Folder

Updated · 11 min read
mediumGit 2.53gitinternalsgit-folderheadobjectsinterview

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:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
ls -F .git
Output
HEAD
config
description
hooks/
info/
objects/
refs/

Seven items, and at this point they are almost empty:

ItemWhat it is for
HEADSays where you are now: which branch you are on
configThe 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
descriptionA 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:

Terminal
cat .git/HEAD
Output
ref: refs/heads/main

It 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:

Terminal
ls .git/refs/heads
find .git/objects -type f
Output
 

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:

Terminal
echo "Fee Tracker" > README.md
git add README.md
find .git/objects -type f
Output
.git/objects/d5/924b0d02d9ef4f29fef5369a7ddcaf82c830c7

One 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

Terminal
git commit -m "Add project README"
Output
[main (root-commit) ae1a9bb] Add project README
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
Terminal
find .git/objects -type f | sort
Output
.git/objects/ae/1a9bbbe6d947ab028ffb6fc2ec60d4326317fc
.git/objects/b5/6cb63ce4c1986636394834fbee61651b8b58d1
.git/objects/d5/924b0d02d9ef4f29fef5369a7ddcaf82c830c7

There are three objects now. (| sort puts the list in alphabetical order.) git cat-file -t prints the type of an object:

Terminal
git cat-file -t d5924b0
git cat-file -t b56cb63
git cat-file -t ae1a9bb
Output
blob
tree
commit
ObjectTypeWhat it holds
d5924b0blobThe content of README.md
b56cb63treeThe snapshot: a list that says "README.md has the content d5924b0"
ae1a9bbcommitThe 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:

Terminal
cat .git/refs/heads/main
Output
ae1a9bbbe6d947ab028ffb6fc2ec60d4326317fc

That 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:

From HEAD to a branch to a commit to your file
  1. HEAD says: you are on refs/heads/main.
  2. refs/heads/main says: the newest commit is ae1a9bb.
  3. The commit ae1a9bb says: my snapshot is the tree b56cb63.
  4. The tree says: README.md has the content in blob d5924b0.

What Else Appeared

List the folder again after the first commit:

Terminal
ls -F .git
Output
COMMIT_EDITMSG
HEAD
config
description
hooks/
index
info/
logs/
objects/
refs/

Three new items:

ItemWhat it is for
indexThe 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_EDITMSGThe 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/pack to 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, and refs/heads may 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 main prints the commit a branch points to, whichever format is in use.
  • The index is a binary file, so cat .git/index is not readable. Use git ls-files --stage to 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
Terminal
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 --oneline
Output
51655eea1a8d3e00e58416fb9164f691f65e2ac6
51655ee (HEAD -> main) Describe what the app does
ae1a9bb Add project README

The 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?

Terminal
find .git/objects -type f | sort
Output
.git/objects/22/3bab126d5f9484c2d82349beacc6fe37c16951
.git/objects/51/655eea1a8d3e00e58416fb9164f691f65e2ac6
.git/objects/ae/1a9bbbe6d947ab028ffb6fc2ec60d4326317fc
.git/objects/b5/6cb63ce4c1986636394834fbee61651b8b58d1
.git/objects/b5/b7c2b6fdfc350d96e7e23b91f859824cdc574a
.git/objects/d5/924b0d02d9ef4f29fef5369a7ddcaf82c830c7

Six: 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.

Quick check: Inside the .git folder

4 questions
1What does the file .git/HEAD normally contain?
2What is a branch, on disk?
3Which command first stores a file's content in .git/objects?
4You delete the .git folder of a project. What is left?

Key Takeaways

Quick reference

CommandIt is used to
ls -F .gitList what is inside the .git folder
cat .git/HEADShow which branch HEAD points to
cat .git/refs/heads/mainShow the commit ID that the branch main holds
find .git/objects -type fList 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?

Lesson 11 of 106