Git internals and hooks
You open up the object database, refs and pack files, and write your own checks with hooks.
Level 301 · 16 lessons · 15 practice labs
Lessons
The object model
- Blob, tree, commit and tag objects
- Reading objects by hand with cat-file and hash-object
- Refs, HEAD and packed-refs
- The index file: inside the staging area
- Pack files, gc and fsck
- Short task: create a commit without porcelain commands
Configuration and attributes
- Configuration levels and includeIf
- .gitattributes: line endings, diff and merge drivers
- Aliases and your own git subcommands
- Short task: define a readable diff for binary files
Hooks
- Client hooks: pre-commit, commit-msg
- Scanning for secrets before commit
- Server hooks: pre-receive, update, post-receive
- Sharing team hooks with core.hooksPath
- Short task: reject non-conforming commit messages on the server
Wrap-up
- Capstone: repair a broken repository at object level and guard it with hooks
Practice labs
- Read a file from the objects: Find the contents of a file in the given commit without checking it out by following the commit → tree → blob chain with cat-file, and write the blob id in your report.
- Build a tag by hand: Create an annotated tag object without porcelain commands, attach it under refs/tags and show with git show that the tag is valid.
- An index that hides changes: Find files that are modified yet invisible in git status using ls-files flags, clear the assume-unchanged and skip-worktree marks and show the real changes.
- Where is this ref stored?: Work out which branches and tags in the repository are loose files and which live in packed-refs, and resolve each with rev-parse into a table.
- Before and after packing: Measure the object count and size of Windy Ridge Energy’s bloated repository, shrink it with gc and repack, and report before/after numbers from count-objects.
- A broken HEAD and a dangling branch: Repair a HEAD file with corrupted content and a branch ref pointing to a missing object by hand, find the right commit from the reflog and show fsck runs clean.
- Where does this setting come from?: Find the settings that make commits open the wrong editor with the wrong email using --show-origin, fix them at the right level and show the result with git config --list.
- A separate identity for the work folder: Set up an includeIf configuration that uses the company email and signing key only for repositories under the work folder, and verify by committing in two different repositories.
- A changelog that always conflicts: Define a merge driver in .gitattributes for the changelog file that keeps lines from both sides, and show three branches merging the changelog without conflicts.
- Catch whitespace errors before commit: Write a pre-commit hook that stops the commit if staged changes contain trailing whitespace or conflict markers, and test it with one bad and one clean attempt.
- Stop before a key leaks: Write a pre-commit hook that looks for cloud access key and private key patterns while letting allowlisted sample values through, and show it blocks the fake key in the lab.
- A team hook that checks only staged content: Enable a shared hooks folder in the repository with core.hooksPath and write a hook that runs the linter only on staged content, proving it gives the right result with partial staging.
- A main branch that can’t be deleted: Install an update hook in the bare repository that refuses deleting or force-overwriting main, and show both attempts are refused.
- A server gate for big files: Write and test a pre-receive hook that refuses a push if any pushed commit contains a file above the size limit, naming the file path.
- Deploy on push: Set up a post-receive hook for Oakmere Studios’ site that checks files out to the publish folder only on pushes to main and keeps a log, proving a push to another branch leaves the site alone.
About this course
You open up the object database, refs and pack files, and write your own checks with hooks.
What you will learn
- The object model
- Configuration and attributes
- Hooks
- Wrap-up
Prerequisites
You should be comfortable with the level 101 and 201 topics of the same path.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 301; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.