Sub topic 1
Plumbing and porcelain: two layers of Git commands
Prerequisites
This chapter assumes you can already do the following:
- Run
git init,git add,git commit,git logandgit branchin a Unix-style shell. - Explain in one sentence what a hash function does: it turns input of any size into a fixed-size fingerprint, and the same input always gives the same fingerprint.
You do not need to know how Git stores data. The rest of this tutorial builds that model step by step.
Two layers
Git ships with well over a hundred commands. They fall into two layers.
- Porcelain commands are the ones you type every day:
git add,git commit,git switch,git merge,git log. They are designed for people. They print readable messages, accept friendly shortcuts, and may change their output between versions. - Plumbing commands are small, single-purpose tools such as
git hash-object,git cat-file,git write-treeandgit update-ref. They are designed for scripts and for other programs. They do one job, take explicit arguments, and keep their output format stable.
The names come from a bathroom. Porcelain is the visible fixture you use. Plumbing is the pipework behind the wall that makes the fixture work.
One porcelain command, several plumbing steps
A single porcelain command often hides several plumbing steps. Roughly, git commit does this:
- It writes the index (the staging area) out as a tree object, as
git write-treedoes. - It creates a commit object that points at that tree and at the current commit, as
git commit-treedoes. - It moves the current branch to the new commit, as
git update-refdoes.
Because the layers are separate, you can run any step by hand. This tutorial does exactly that. Doing it once removes much of Git's mystery, because every later behavior (branching, merging, rebasing) is a rearrangement of the same few object types.
When each layer is useful
| Situation | Better layer | Why |
|---|---|---|
| Everyday work: staging, committing, merging | Porcelain | Friendly messages and safe defaults |
| Writing a script or a tool | Plumbing | Stable output that is safe to parse |
| Debugging a strange repository state | Plumbing | Shows what is actually stored |
| Learning how Git works | Plumbing | Each command exposes one concept |
The boundary is not perfectly clean. Some commands act as both. git log is porcelain, but git rev-list does similar work with script-friendly output. git status has a --porcelain option that switches to a stable, machine-readable format.
git status
git status --porcelainThe first form is meant for you to read. The second is meant for a script, and its format will not change from one Git version to the next.
A first look
Run this in any repository that has at least one commit. It uses one plumbing command to read what the porcelain git log normally shows you.
git rev-parse HEAD
git cat-file -t HEAD
git cat-file -p HEADgit rev-parse HEAD prints the full hash of the current commit. git cat-file -t HEAD prints its type, which is commit. git cat-file -p HEAD prints the raw commit: a tree hash, parent hashes, author, committer and message. You will learn each of those fields in later chapters.
Exercise
You want a shell script to list which files changed in the working directory. Should it parse the output of git status or git status --porcelain, and why?
Show answer
It should use git status --porcelain. That format is documented and stable across Git versions, while the default output is meant for humans and can change its wording or layout.
Exercise
Which three plumbing steps does git commit roughly perform, in order?
Show answer
It writes the index as a tree (git write-tree), creates a commit object pointing at that tree and the parent commit (git commit-tree), and moves the current branch to the new commit (git update-ref).