Sub topic 10
Building a commit by hand
This chapter assumes you know blobs, trees, and commits as separate object types, and that you can use git cat-file. Here you create all three with plumbing commands (low-level commands that each do one job) and then move a branch to the result. git commit does the same steps for you.
The four steps
A commit needs a tree, and a tree needs blobs. So you build from the bottom up:
git hash-object -wwrites the file contents as a blob and prints its ID.git mktreereads a tree listing and writes a tree object.git commit-treewrites a commit that points to the tree.git update-refmoves the current branch to the commit.
Running it
Start in an empty repository. commit-tree records an author, so set an identity first.
git init demo && cd demo
git config user.name "Demo User"
git config user.email "[email protected]"
blob=$(echo 'hello' | git hash-object -w --stdin)
tree=$(printf '100644 blob %s\tgreeting.txt\n' "$blob" | git mktree)
git cat-file -p "$tree"
commit=$(echo 'first commit' | git commit-tree "$tree")
git update-ref HEAD "$commit"
git cat-file -p "$commit"
git log --onelineEach command prints an ID, and the script stores it in a shell variable for the next step.
What each step does
The printf line is the tree listing. Each line has a mode, a type, an object ID, a tab, and a name. Mode 100644 means a regular, non-executable file. The tree is where the name greeting.txt lives. The blob knows nothing about it.
git cat-file -p "$tree" shows the entry back to you, so you can check it before building on it.
commit-tree takes the tree ID and reads the message from standard input. It prints the new commit's ID but changes no branch. The commit has no parent because you passed none. To add one, use -p <parent-id>.
update-ref HEAD "$commit" is the step that makes the commit visible to git log. HEAD normally points to a branch name, and update-ref follows that pointer. It writes the commit ID into the branch file, such as .git/refs/heads/master or main, depending on your default branch name. Until you do this, the commit exists in the object database but no ref reaches it.
Common errors
- "Author identity unknown" or "empty ident name":
commit-treeneedsuser.nameanduser.email. Set them as shown above. mktreefails with a format error: The separator between the ID and the filename must be a real tab. Inprintf, write\t. A space does not work.git statusshowsgreeting.txtas a staged deletion: You moved the branch but never touched the index (the staging area) or the working tree. The commit contains the file, while the index and the directory are empty, so Git sees the file as removed. Rungit reset --hardin this demo to bring both in line withHEAD. Do not run it in a repository with work you want to keep.- The commit seems to disappear: If you skip
update-ref, nothing points to the commit, and garbage collection can eventually delete it.
Exercise
You run commit-tree on a valid tree and get an ID, but git log shows nothing new. Which command is missing, and why does it fix the problem?
Show answer
git update-ref HEAD <commit-id> is missing. commit-tree only writes the commit object. update-ref writes its ID into the branch that HEAD points to, so git log can reach it.
Exercise
Why does the tree entry contain the filename while the blob does not?
Show answer
A blob stores only content, so two files with identical content share one blob. The tree maps names and modes to object IDs, so it holds the filename.