Sub topic 11
Building a commit by hand
This chapter assumes you know basic Git (init, add, commit, log, branch) and can use a Unix-style shell. It also assumes you know that a blob stores file contents, a tree stores names and modes, and a commit points at a tree. You will now chain the plumbing commands that git commit runs for you.
The four steps
A porcelain git commit does four jobs. Each has a plumbing command:
- Store file contents as blobs and record names in the index:
git hash-object -wandgit update-index. - Turn the index into a tree object:
git write-tree. - Wrap the tree in a commit object:
git commit-tree. - Move the branch to the new commit:
git update-ref.
The first commit
Start from a repository that already holds the blob for hello in $id, as in the earlier chapters. Add it to the index under a name, then write the tree.
git update-index --add --cacheinfo 100644 "$id" hello.txt
git ls-files --stage
tree=$(git write-tree)
git cat-file -p "$tree"100644 is the mode for a regular file. write-tree prints the hash of the new tree and stores it in the object database.
Now create the commit. commit-tree reads the message from standard input and prints the new commit's hash. It needs user.name and user.email to be set, because it records an author and committer.
commit=$(echo 'first commit' | git commit-tree "$tree")
git cat-file -p "$commit"The output shows the tree line, the author and committer lines, and the message. There is no parent line, because this is a root commit.
At this point the commit exists, but nothing refers to it. git log shows nothing useful yet. Give it a name:
git update-ref refs/heads/main "$commit"
git symbolic-ref HEAD refs/heads/main
git log --onelineupdate-ref writes the hash into .git/refs/heads/main. symbolic-ref makes HEAD contain ref: refs/heads/main, so HEAD follows the branch.
The second commit
A second commit differs only in that you name its parent with -p.
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 refs/heads/main "$commit2"
git cat-file -p HEAD
git cat-file -p 'HEAD^{tree}'Here update-index --add second.txt has no --cacheinfo, so Git reads the file, writes the blob, and stages it in one step. The new tree lists both files. The first file's blob is reused, since its content is unchanged and so is its hash.
HEAD now resolves through refs/heads/main to commit2. The suffix ^{tree} asks for the tree that commit points to.
Common errors
fatal: empty ident nameor "unable to auto-detect email address": setuser.nameanduser.emailwithgit config.write-treereports missing objects: the index names a blob that is not in the object database. This happens when you use--cacheinfowith a hash you never wrote withhash-object -w.- A commit you cannot find in
git log: you skippedupdate-ref. Without a ref, the commit is unreachable. - A wrong tree in the second commit:
write-treesnapshots the whole index, not only the file you just added.
Exercise
You run git commit-tree "$tree2" -p "$commit" and save the printed hash, but git log still shows the old history. Which command is missing, and what does it change on disk?
Show answer
git update-ref refs/heads/main <new hash> is missing. It writes the new commit's hash into the file .git/refs/heads/main, so the branch, and HEAD through it, points at the new commit.
Exercise
In the second commit, why does the tree contain the same blob hash for hello.txt as the first tree?
Show answer
Git names objects by a hash of their content. The contents of hello.txt did not change, so its blob has the same hash and is stored once. Both trees point at it.