Sub topic 11
Building a commit by hand
This chapter assumes you have read the earlier chapters on blobs, trees, commits, refs and the index. It also assumes you can read git cat-file -p output and run commands in a Unix-style shell. Here you will combine those pieces and make a commit without git add or git commit.
The four steps behind git commit
A porcelain git commit hides four separate operations. Each one has a plumbing command.
- Put file contents into the object database as blobs (
git hash-object -w). - Record names, modes and blob IDs in the index (
git update-index). - Turn the index into a tree object (
git write-tree). - Wrap the tree in a commit object (
git commit-tree), then move a branch to it (git update-ref).
Only the first and fourth steps write something that has a name you recognize. The middle steps produce objects and IDs that you must capture in shell variables and pass along yourself.
Set up a repository
Start in a fresh repository. The commands below set an identity for this repository only. They also point HEAD at main, so the result does not depend on your init.defaultBranch setting.
git init -q plumbing-demo
cd plumbing-demo
git config user.name "Demo User"
git config user.email "[email protected]"
git symbolic-ref HEAD refs/heads/maingit symbolic-ref HEAD refs/heads/main writes ref: refs/heads/main into .git/HEAD. The branch main does not exist yet, because .git/refs/heads/main is missing. Git calls this an unborn branch. HEAD names a branch that has no commit.
Write the first commit
Run the whole sequence once, then read the notes after it.
id=$(echo 'hello' | git hash-object -w --stdin)
git update-index --add --cacheinfo 100644 "$id" hello.txt
tree=$(git write-tree)
commit=$(echo 'first commit' | git commit-tree "$tree")
git update-ref refs/heads/main "$commit"
git log --oneline
git cat-file -p "$commit"The notes below explain each step.
hash-object -wwrites the blob and prints its ID. Without-wit only computes the ID. The blob forhello\nalways has the IDce013625030ba8dba906f756967f9e9ca394464a.--cacheinfo 100644 "$id" hello.txtcreates an index entry from three values you supply: the mode, the blob ID, and the path. No file calledhello.txthas to exist in the working directory.100644means a regular, non-executable file.write-treereads the index and writes one tree object for each directory level. It prints the ID of the root tree.commit-treetakes a tree ID and reads the commit message from standard input. It prints the new commit's ID and does nothing else. No branch moves.update-refis what makes the commit visible. It writes the commit ID into.git/refs/heads/main, andHEADalready points at that branch.
The output of git cat-file -p "$commit" has a tree line, an author line, a committer line, a blank line and the message. It has no parent line, because this is a root commit.
What the working tree shows
Run git status --short. You will see this:
D hello.txtThe index and HEAD agree that hello.txt exists. The working directory has no such file, so Git reports it as deleted. Your plumbing commands never touched the working directory. To restore the file, run git restore hello.txt. That is a porcelain command that copies the blob out of the index.
Write a second commit with a parent
A commit gets a parent only if you pass -p. You can pass it more than once, and each use adds one parent line. A merge commit has two or more.
echo 'second' > second.txt
git update-index --add second.txt
tree2=$(git write-tree)
commit2=$(echo 'second commit' | git commit-tree "$tree2" -p "$commit")
git update-ref -m "second commit by hand" refs/heads/main "$commit2" "$commit"
git cat-file -p HEAD
git cat-file -p 'HEAD^{tree}'
git rev-parse HEAD "$commit2"This time update-index --add second.txt reads the file from the working directory. It hashes the contents, writes the blob, and adds the index entry in one command. The two forms are equivalent. --cacheinfo is the lower-level form for when you already have a blob ID.
The new tree has two entries: hello.txt and second.txt. The first entry is still in the index from the previous section, so it carries over.
The last argument to update-ref is the old value. Git moves the branch only if it currently points at $commit. This is a compare-and-swap check. It protects you if another process moved the branch between your steps. -m records a message in the reflog.
The final rev-parse prints the same ID twice, which confirms that HEAD resolves to the commit you built.
How HEAD resolves
HEAD is a symbolic reference. Git follows it in two hops. The table shows what each file holds at this point.
| Location | Contents | Meaning |
|---|---|---|
| `.git/HEAD` | `ref: refs/heads/main` | The current branch is `main`. |
| `.git/refs/heads/main` | the ID of `$commit2` | The branch points at the second commit. |
| the second commit | `tree` line and `parent` line | Its tree is `$tree2`, and its parent is `$commit`. |
The suffix ^{tree} in HEAD^{tree} tells Git to follow the reference to a commit and then take that commit's tree. This is why git cat-file -p 'HEAD^{tree}' lists the files directly. Quote the argument so the shell does not treat { or ^ specially.
Common errors
Most failures in this workflow come from the same few mistakes.
| Symptom | Cause | Fix |
|---|---|---|
| `Author identity unknown` or `empty ident name not allowed` | `user.name` or `user.email` is not set, and `commit-tree` needs both. | Run `git config user.name` and `git config user.email`, or set `GIT_AUTHOR_NAME` and `GIT_AUTHOR_EMAIL`. |
| `git write-tree` fails with `invalid object ... for 'hello.txt'` | The blob was never written, usually because `-w` was left off `hash-object`. | Rerun `hash-object -w` on the same content, then `write-tree`. |
| `commit-tree` says the ID is not a valid `tree` object | You passed a commit or blob ID instead of a tree ID. | Pass the output of `write-tree`, or use `"$commit^{tree}"`. |
| `update-ref` says it cannot lock the ref because it is at one ID but expected another | The old value you gave does not match the branch's current value. | Look up the current value with `git rev-parse refs/heads/main` and decide whether to retry. |
| `git log` says the current branch has no commits | `update-ref` never ran, or it ran against a different branch name than `HEAD` names. | Run `git symbolic-ref HEAD` and update that ref. |
| The commit is missing from every branch | You ran `commit-tree` and stopped. | Run `update-ref`. Until then, `git fsck` lists the commit as dangling. |
Check your work
Two commands confirm the repository is consistent.
git fsck
git log --stat --format='%h %s'git fsck should print nothing, or only a note about dangling objects you created on purpose. git log --stat shows hello.txt added in the first commit and second.txt added in the second.
Because you built the commits with the same inputs porcelain uses, nothing marks them as unusual. git checkout, git branch and git push treat them like any other commits.
Exercise
You run commit=$(echo 'try' | git commit-tree "$tree") and then git log --oneline, which still shows the old history. The object $commit exists and git cat-file -p "$commit" works. What step is missing, and which command performs it?
Show answer
The branch ref has not moved. commit-tree only writes the commit object. You need git update-ref refs/heads/main "$commit", where refs/heads/main is the branch that HEAD names. Until then the commit is unreachable from any ref, and git fsck reports it as dangling.
Exercise
You want a commit that has the same files as $commit2 but a different message and the same parent as $commit2. Which command makes it, and what does git cat-file -p "$commit2" tell you that you need first?
Show answer
Read the parent ID from the parent line of git cat-file -p "$commit2", or use "$commit2^". Then run echo 'new message' | git commit-tree "$commit2^{tree}" -p "$commit2^". This reuses the tree and the parent and gives a new commit with a different ID, because the message is part of the hashed content.