Sub topic 1
Plumbing and porcelain: two layers of Git commands
Git is a set of small programs, and the commands you type every day are built from them. This chapter explains the two layers, shows how to tell them apart, and runs a short experiment that looks at an ordinary commit from the lower layer. The rest of the tutorial uses the lower layer to open up the .git directory.
Prerequisites
This chapter assumes you have:
- Basic Git use:
init,add,commit,logandbranch. - Comfort with a Unix-style shell. You should be able to run commands, use
$(...)to capture output and read a shortshscript. - A rough idea of what a hash function does: it turns any input into a fixed-size fingerprint, and the same input always gives the same fingerprint.
You need Git 2.28 or later for git init -b. Any Linux, macOS or Git Bash shell works.
Two layers
The Git documentation splits commands into two groups.
- Porcelain commands are the high-level, user-facing commands. Examples are
git add,git commit,git switch,git merge,git logandgit status. They are meant for people, so they print friendly text, apply defaults, use color and sometimes change their output between versions. - Plumbing commands are the low-level building blocks. Examples are
git hash-object,git cat-file,git update-index,git write-tree,git commit-treeandgit update-ref. Each does one narrow job on the repository's data. They are meant for scripts and for other programs, so their output format is kept stable.
The names come from plumbing in a building. Porcelain is the sink and toilet you use. Plumbing is the pipework behind the wall that makes them work.
Most porcelain commands are composed from plumbing. git commit reads the index, calls the equivalent of write-tree, creates a commit object, and moves a branch. Early versions of Git were mostly plumbing, and the porcelain was added on top as shell scripts. Many porcelain commands are now written in C for speed, but the layering is still how Git is designed and documented.
How to tell which layer a command belongs to
There is no flag that marks a command, but three sources give you the answer.
- The manual page
git help githas separate sections called "High-level commands (porcelain)" and "Low-level commands (plumbing)". The plumbing section is further split into manipulators, interrogators and sync commands. git help -alists every available command in groups.- The name often gives a hint. Plumbing names tend to be a verb and an object joined by a hyphen, such as
hash-object,read-tree,ls-filesorrev-parse. Porcelain names are usually a single everyday word, such asaddormerge.
The hint is not reliable. git tag and git branch are porcelain, and git rev-list is plumbing. When unsure, check the manual page.
Comparing the layers
| Aspect | Porcelain | Plumbing |
|---|---|---|
| Intended user | A person at a terminal | A script or another program |
| Output | Readable, may be colored, may change between versions | Stable and easy to parse |
| Defaults | Many conveniences and safety checks | Few or none |
| Scope of one command | A whole task, such as "commit my work" | One small step, such as "write this tree" |
| Examples | `add`, `commit`, `switch`, `log`, `status` | `hash-object`, `cat-file`, `update-ref`, `ls-tree` |
A porcelain command is a good choice when you are working interactively. A plumbing command is a better choice when you are writing a script that must keep working after a Git upgrade, or when you need to see exactly what is stored.
The confusing `--porcelain` flag
Some porcelain commands have an option that gives them script-friendly output. For example:
git status --porcelain=v1This prints one short line per changed file in a format that is guaranteed not to change. The flag is named after the stable layer, but git status itself is still a porcelain command. It is a good middle ground for scripts when no plumbing command does the same job. The default git status text, in contrast, is for human eyes and should never be parsed.
An experiment: a normal commit seen through plumbing
Make a repository and one commit with porcelain commands only.
git init -b main layers-demo
cd layers-demo
echo 'hello' > hello.txt
git add hello.txt
git commit -m 'first commit'If git commit complains about your identity, set it first with git config user.name "Your Name" and git config user.email "[email protected]".
Now ask the same repository questions using plumbing commands.
git rev-parse HEAD
git cat-file -t HEAD
git cat-file -p HEAD
git ls-files --stage
git ls-tree HEAD
git symbolic-ref HEADYou will see this:
rev-parse HEADprints a 40-character hash. That is the full name of your commit.cat-file -t HEADprintscommit.cat-file -p HEADprints the commit's tree hash, your name and email, and the message.ls-files --stageshows the index entry forhello.txt, with its mode and blob hash.ls-tree HEADshows the same mode, blob hash and name, now stored in the commit's tree.symbolic-ref HEADprintsrefs/heads/main.HEADis not a commit hash. It is a pointer to a branch, and the branch points to the commit.
Behind one git add and one git commit, Git created a blob, updated the index, wrote a tree, wrote a commit, and moved a branch. The next chapters make each of these steps by hand.
A porcelain-to-plumbing map
The mapping below is approximate. Real implementations do more checking, but it shows where each everyday command ends up.
| Porcelain command | Roughly equivalent plumbing steps |
|---|---|
| `git add file` | `hash-object -w`, then `update-index --add` |
| `git commit` | `write-tree`, then `commit-tree`, then `update-ref` |
| `git branch name` | `update-ref refs/heads/name` |
| `git status` | `diff-index`, `diff-files` and `ls-files` |
| `git log` | `rev-list` plus reading commit objects with `cat-file` |
| `git switch name` | `read-tree` to update the index, then `symbolic-ref HEAD` |
Using plumbing in a script
The script below reports the current branch, the short commit hash, and whether tracked files differ from HEAD. It uses only commands whose output is meant for scripts.
#!/bin/sh
set -eu
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "not inside a Git work tree" >&2
exit 1
fi
branch=$(git symbolic-ref --short -q HEAD || echo "(detached)")
commit=$(git rev-parse --short HEAD)
git update-index -q --refresh
if git diff-index --quiet HEAD --; then
state=clean
else
state=modified
fi
echo "$branch $commit $state"Save it as repo-state.sh inside the demo repository and run sh repo-state.sh. It prints main followed by a short hash and clean. Edit hello.txt and run it again to see modified.
The script shows several habits that plumbing work needs.
- Exit codes carry the answer.
diff-index --quietprints nothing. It reports the result through its exit status, which is how plumbing commands usually talk to scripts. symbolic-ref -qis quiet whenHEADis detached. It fails silently, so the|| echofallback runs. Without-qit prints an error message.- The
--separator afterHEADtells Git that nothing after it is a revision. This avoids confusion when a file name looks like a branch name. update-index --refreshcomes first. Git caches file timestamps in the index. If it does not refresh them,diff-indexcan report a change on a file whose content is unchanged, for example after atouch.
Common errors at this stage
- Parsing porcelain output in scripts. Scraping the text of
git logorgit statusbreaks when Git changes wording, when the user's locale is not English, or when color is on. Use plumbing, or a porcelain command with a stable format option such as--porcelain. - Assuming the script sees untracked files.
diff-indexcompares tracked files only. A new file that was never added does not make the state "modified". If you need untracked files, usegit status --porcelainorgit ls-files --others --exclude-standard. - Running
rev-parse HEADbefore any commit exists. In a new repositoryHEADpoints at a branch that does not exist yet, so the command fails. Withset -e, the script then stops. - Treating
HEADas a commit hash.HEADis usually a symbolic reference to a branch. It holds a hash directly only in the detached state. - Forgetting to quote variables. Write
"$id", not$id, so empty or unusual values do not change how the command is parsed.
Exercise
You want a script that lists every file name in the latest commit, one per line. Why is git ls-tree -r --name-only HEAD a better choice than parsing the output of git show --stat?
Show answer
ls-tree is a plumbing command, so its output format is stable and meant for scripts. git show --stat is porcelain. Its output is meant for people and can change between versions, include colors, and truncate long file names to fit the terminal width. ls-tree -r also recurses into subdirectories and lists only files, not directories.
Exercise
After running git commit, git symbolic-ref HEAD prints refs/heads/main, but git rev-parse HEAD prints a 40-character hash. Why do the two commands give different kinds of answers?
Show answer
symbolic-ref shows what HEAD itself contains. HEAD is normally a pointer to a branch name, so the answer is the name of a ref. rev-parse resolves a name all the way down to an object hash, following HEAD to the branch and then to the commit the branch points at.