Git for Engineers — Level by Level›01 · How Git works

Lesson 01 of 8 · Level 1 — Foundations

How Git works

Git makes sense once you see what it stores: snapshots called commits, linked to their parents, with branches as movable labels and HEAD pointing at where you are. Set Git up and learn the three areas every command moves files between.

Beginner
Key wordsgitrepositorycommitsnapshotSHAtreeblobbranchHEADworking treestaging areaindexgit config
Commits: snapshots linked to their parents commit a tree + parent commit b tree + parent commit c tree + parent commit d tree + parent commit e on feature main feature HEAD -> main Three areas working tree -> staging area -> repository (commits) branches and HEAD are labels; commits never change
Commits are snapshots linked to their parents; branches and HEAD are just labels pointing at commits.

What Git actually stores

Git is a small database of objects, each named by the hash (SHA) of its content:

Object Holds
blob The contents of one file
tree A directory listing: names pointing to blobs and other trees
commit One tree (the full snapshot), the parent commit(s), author, date and message
tag (annotated) A named, signed-able pointer to a commit, with a message

Every commit is a snapshot of the entire project, not a list of changes. Files that didn't change are simply the same blob reused, so history stays compact. Because a commit's SHA includes its parent's SHA, history forms a chain that can't be quietly altered.

Think of a photo album where every page is a full photo of your room, not a note saying "moved the chair". Each photo has a sticker saying which photo came before it. Bookmarks (branches) mark the pages you care about, and a finger (HEAD) shows which page you're looking at right now.

Refs: branches, tags and HEAD

  • A branch is a file (.git/refs/heads/main) containing one commit SHA. Commit on it and it moves forward.
  • A tag is a label that doesn't move, typically for releases (v1.4.0).
  • HEAD says where you are: usually "on branch main". If it points directly at a commit instead, you're in detached HEAD state (common in CI checkouts).
$ cat .git/HEAD
ref: refs/heads/main
$ cat .git/refs/heads/main
3f1c9a0b7e2d4c5a8b9e1f2d3c4b5a6978e1d2c3
$ git cat-file -p HEAD
tree 9b2e1c...
parent 7a4d3f...
author Asha <asha@example.com> 1790000000 +0530
committer Asha <asha@example.com> 1790000000 +0530

Add readiness probe to api

The three areas

Nearly every Git command moves changes between three places:

Area What it is Commands that move changes
Working tree The files you edit edit, git restore <file> (discard)
Staging area (index) The next snapshot being prepared git add, git restore --staged
Repository (.git) Committed snapshots git commit, git checkout/switch

The staging area is what lets you commit part of your changes: one logical change per commit, even if you edited five things.

Set Git up properly

$ git config --global user.name "Asha Rao"
$ git config --global user.email asha@example.com
$ git config --global init.defaultBranch main
$ git config --global pull.rebase true          # linear history when pulling
$ git config --global fetch.prune true          # remove deleted remote branches
$ git config --global core.editor "vim"

Use the email your Git server knows, so commits link to your account. On shared machines, set identity per repository (git config user.email ... without --global).

Try it: look under the hood

  1. git init demo && cd demo, create app.txt, git add app.txt, git commit -m "First".
  2. Run git cat-file -p HEAD, then git cat-file -p HEAD^{tree}, then git cat-file -p <blob-sha>: commit → tree → file content.
  3. Change the file, commit again, and compare the two commits' parent lines.
  4. git branch feature and cat .git/refs/heads/feature: a branch is just a SHA in a file.
  5. git checkout <first-commit-sha> and run git status: note "HEAD detached". Return with git switch main.

Going deeper: why Git is fast and safe

Because objects are content-addressed, Git can check integrity (a corrupted object no longer matches its name), deduplicate identical files automatically, and compare two snapshots by comparing tree hashes, skipping whole unchanged directories. Objects are later packed together with delta compression (git gc), which is where "diffs" appear in storage, as an optimisation rather than the model. Newer Git versions can use SHA-256 object names, but SHA-1 repositories remain the default on most hosting platforms.

Recap

  • A commit is a full snapshot (tree) + parents + metadata, named by its hash.
  • Branches and tags are labels; HEAD says where you are.
  • Changes move working tree → staging area → repository.
  • Set name, email, default branch and pull behaviour once with git config --global.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.