Skip to content
Visual explainer 2h to learn 3 steps

How Git works: objects, commits, HEAD

Explain what a commit, branch and HEAD actually are.

By Invicly Tech, , in Git, GitHub & GitHub Actions. About 11 minutes to read.

Topic 1 of 18

In short

Git stores your project as a small set of objects: blobs for file contents, trees for folders and commits for snapshots. Branches and HEAD are just pointers to commits, which is why Git operations are fast and recoverable.

Think of it like this: Commits are photos in an album; branches are sticky notes marking a page.

Explained simply

Git is a small database that lives in a hidden folder called .git inside your project. When you commit, Git does not save a list of changes. It saves a full snapshot of every file at that moment. To keep this cheap, files that did not change are not copied again; the new snapshot simply points to the old copy.

Everything in that database is an object with a name made from its content. Git runs the content through a hash function (a formula that turns any data into a fixed-length code, like a fingerprint). Same content, same fingerprint. Different content, different fingerprint. This is why a commit ID looks like 4f2a9c1e... and why two identical files are stored only once.

There are three main object types. A blob holds the contents of one file. A tree is a folder listing that maps names to blobs and other trees. A commit points to one tree (the whole project) plus its parent commit, the author and the message. Follow the parents and you walk back through history.

Branches and HEAD are not objects at all. A branch is a tiny text file that holds one commit ID. HEAD is a file that says which branch you are on. When you commit, Git writes the new objects and then moves the branch to point at the new commit. That is all a branch ever does.

Press Play on a diagram to walk through it one step at a time.

LayersFrom HEAD down to file contents
  1. 1
    HEAD

    Points to the current branch, e.g. refs/heads/main.

  2. 2
    Branch main

    A file holding one commit hash; moves on each commit.

  3. 3
    Commit object

    Author, message, parent commit hash and one root tree.

  4. 4
    Tree object

    A folder listing: names mapped to blobs and subtrees.

  5. 5
    Blob object

    Raw file contents, named by the hash of that content.

MapOne commit, unpacked
commit 9fceb02
  • tree a1b2c3 (project root)
    • blob: README.md
    • blob: app.py
    • tree: tests/
  • parent 4f2a9c1
    • The previous commit
    • Its own tree and parent
  • author and committer
    • Name, email, timestamp
  • message
    • Add health check
ComparePorcelain vs plumbing commands
Porcelain (daily use)
  • git add, commit, log
  • Friendly output
  • Output may change between versions
  • Made for people
Plumbing (low level)
  • hash-object, cat-file, write-tree
  • Raw, stable output
  • Safe to use in scripts
  • Shows how Git really works

In depth

How an object gets its name

Git builds a short header, the object type, a space, the size in bytes and a zero byte, puts it in front of the content, and hashes the result with SHA-1. The 40 hex characters you see are that hash. The object is then compressed with zlib and stored in .git/objects, in a folder named after the first two characters, with the other 38 as the file name.

Because the name comes from the content, objects can never change. Edit one byte and you get a different object with a different name. A commit contains its tree hash and its parent hash, so changing anything in the past changes every commit after it. That is what makes Git history tamper-evident. Newer Git versions can also create SHA-256 repositories with git init --object-format=sha256, but most hosting still uses SHA-1.

  • echo hello | git hash-object --stdin prints ce013625030ba8dba906f756967f9e9ca394464a
  • The same content always gives the same hash, on any machine
  • git cat-file -s HASH prints the size of an object

The staging area is a real file

Between your working folder and the object database sits the index, a binary file at .git/index. It lists every tracked path with the blob hash and file mode that will go into the next commit. git add does two things: it writes a blob for the file's current content and updates that path's entry in the index.

git commit then turns the index into tree objects (one per folder), writes a commit that points to the top tree and the current commit as parent, and moves the branch. This is why you can stage only part of your changes: the index is a draft of the next snapshot, separate from both your files and the last commit.

FlowWhat git add and git commit write
  1. 1
    Edit a file

    Only your working folder changes

  2. 2
    git add

    Writes a blob and updates .git/index

  3. 3
    git commit

    Turns the index into tree objects

  4. 4
    New commit object

    Points to the top tree and the parent

  5. 5
    Branch moves

    refs/heads/main now holds the new hash

Loose objects, packfiles and garbage collection

New objects are first written as loose objects, one compressed file each. That is simple but wasteful when a big file changes a little in every commit. From time to time Git runs garbage collection (git gc, often started automatically) which packs objects into a packfile. Inside a pack, Git stores similar objects as deltas, meaning one full copy plus small differences.

So Git is snapshot-based in its model but delta-compressed on disk. Objects that nothing points to any more (for example after you amend a commit) are not deleted at once. They stay reachable through the reflog for weeks and are only pruned after they expire, which is why mistakes are usually recoverable.

  • git count-objects -vH shows loose and packed object counts and sizes
  • Packfiles live in .git/objects/pack
  • git fsck checks every object for corruption

Refs, symbolic refs and detached HEAD

A ref is a name for a commit. Local branches live under refs/heads, remote-tracking branches like origin/main under refs/remotes, and tags under refs/tags. Many refs may be packed into one file, .git/packed-refs, so do not be surprised if a branch file seems missing. Use git rev-parse main to resolve any name to its hash.

HEAD is usually a symbolic ref: its content is ref: refs/heads/main. When you check out a tag or a commit hash directly, HEAD holds a hash instead. That is detached HEAD. You can look around and even commit, but no branch moves, so new commits are only remembered by HEAD itself. Run git switch -c new-name to give them a branch before you leave.

Learn it in 3 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Learn blobs, trees and commit objects~40m
  2. 2
    Explore .git with cat-file~40m
  3. 3
    Understand branches and HEAD as pointers~40m

Key points

  • Every object is named by a hash of its content, so identical files are stored once.
  • A commit points to one tree and to its parent commit, forming a history chain.
  • A branch is a pointer to a commit; creating one is nearly free.
  • Detached HEAD means HEAD points straight at a commit instead of a branch.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
cat .git/HEAD                 # ref: refs/heads/main
cat .git/refs/heads/main      # a commit hash
git cat-file -t HEAD          # commit
git cat-file -p HEAD          # tree, parent, author, message
git cat-file -p HEAD^{tree}   # files and subtrees
git cat-file -p HEAD:README.md

Walk from HEAD down to a file's contents.

Build a commit by hand with plumbing commands

To prove that a commit is just objects plus a moving pointer, make one without git add or git commit. Step 1: write a blob from a file. Step 2: put it in the index. Step 3: turn the index into a tree. Step 4: make a commit object that points to that tree. Step 5: move the current branch to the new commit. git log now shows a normal commit. You need user.name and user.email set for commit-tree to work.

bash
git init demo && cd demo
echo "hello" > a.txt
git hash-object -w a.txt          # ce01362... the blob
git update-index --add a.txt      # stage it in .git/index
tree=$(git write-tree)            # tree object from the index
commit=$(echo "first" | git commit-tree "$tree")
git update-ref HEAD "$commit"     # move the current branch
git log --oneline                 # shows: <hash> first

Why a renamed file costs almost nothing

You rename config.yml to settings.yml and commit. Step 1: Git hashes the content, which did not change, so the blob hash is identical. Step 2: the new tree lists the name settings.yml pointing at that same blob. Step 3: the only new objects are one tree and one commit. Git does not record renames at all; git log --follow and git diff -M detect them later by noticing that a blob disappeared under one name and appeared under another.

bash
git mv config.yml settings.yml
git commit -m "Rename config"
git rev-parse HEAD~1:config.yml   # same hash...
git rev-parse HEAD:settings.yml   # ...as this one
git show --stat -M HEAD           # reported as a rename

Quick reference

  • git cat-file -t HASH prints the object type; -p pretty-prints its content
  • git rev-parse HEAD turns any name (branch, tag, HEAD~2) into a full hash
  • git ls-tree -r HEAD lists every file in the current snapshot with its blob hash
  • git ls-files -s shows what the index (staging area) holds right now
  • Objects live in .git/objects; branches in .git/refs/heads or .git/packed-refs
  • HEAD containing ref: refs/heads/x means on branch x; a bare hash means detached
  • git switch -c rescue saves commits made in detached HEAD
  • git count-objects -vH shows how big the object database is

Common mistakes

  • Thinking a branch contains commits; it only points at the newest one.
  • Panicking in detached HEAD; create a branch there to keep new work.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1What does a blob object store?

Question 2

2You change one old commit's message. What happens to the commits after it?

Question 3

3What does git add actually do?

Question 4

4You are in detached HEAD and make two commits, then switch to main. Where are those commits?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    In any repo, find the hash of the tree for your latest commit and list the files in it using only cat-file.

  2. Exercise 2medium

    Show that two files with identical content are stored as one blob, even in different folders.

  3. Exercise 3hard

    Make a commit in detached HEAD, switch back to main, then recover the commit onto a new branch without knowing its hash in advance.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Does Git store diffs or full copies of files?
In its model, every commit is a full snapshot. On disk, packfiles compress similar objects as deltas, so you get the simplicity of snapshots with the size of diffs.
Is SHA-1 in Git still safe?
Git uses a hardened SHA-1 that detects the known collision attack, and it supports SHA-256 repositories. For everyday use the hash is about integrity, and signed commits or tags add proof of who made them.
What is the difference between HEAD, HEAD~1 and HEAD^?
HEAD is your current commit. HEAD~1 and HEAD^ both mean its first parent; they differ only on merge commits, where HEAD^2 means the second parent and HEAD~2 means the grandparent.
Can I just copy the .git folder to back up a repo?
Yes, it holds the whole history. git clone --mirror or git bundle create are cleaner ways to make a portable copy.

Go deeper

ShareXLinkedInWhatsApp