Build a Commit by Hand
Building a commit by hand means creating every object and reference of a commit yourself, using only plumbing commands, instead of git add and git commit. It takes five commands: hash-object stores a file's content as a blob, update-index puts it in the index, write-tree turns the index into a tree, commit-tree creates a commit for that tree, and update-ref points a branch at it. The result is a normal commit that every porcelain command accepts. Doing it once is the best way to see that the previous lessons describe exactly how Git works.
A cook who has always used a bread machine learns a lot by making one loaf by hand: measuring, kneading, proving and baking each step themselves. Afterwards, the machine is no longer a mystery: it does the same steps, in the same order.
The Five Steps
| Step | Plumbing command | Porcelain that does it for you |
|---|---|---|
| 1. Store the content | git hash-object -w <file> | git add |
| 2. Stage it | git update-index --add --cacheinfo <mode>,<id>,<path> | git add |
| 3. Make a tree | git write-tree | git commit |
| 4. Make a commit | git commit-tree <tree> [-p <parent>] -m "<message>" | git commit |
| 5. Move the branch | git update-ref refs/heads/<branch> <commit> | git commit |
Step 1: Store the Content
An empty repository, and one file:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git hash-object -w fees.txt
find .git/objects -type f2de12a7bc775374fc0d44b3171f096fd4a1fc80a
.git/objects/2d/e12a7bc775374fc0d44b3171f096fd4a1fc80aExplanation: git hash-object computed the blob ID from the content, and -w wrote the blob into the database. Nothing else knows about it yet: it isn't staged, and git status still shows fees.txt as untracked.
Step 2: Stage It
git update-index --add --cacheinfo 100644,2de12a7bc775374fc0d44b3171f096fd4a1fc80a,fees.txt
git ls-files --stage
git status -s100644 2de12a7bc775374fc0d44b3171f096fd4a1fc80a 0 fees.txt
A fees.txtExplanation: --cacheinfo takes a mode, a blob ID and a path, and adds that entry to the index. The file is now staged (A ), exactly as git add would have left it. Note that the index took the ID, not the file: you could stage content that isn't on disk at all.
Step 3: Make a Tree
git write-tree
git cat-file -p $(git write-tree)c2d44c59023fef48017eb9c7ffa24fed5b3e13a6
100644 blob 2de12a7bc775374fc0d44b3171f096fd4a1fc80a fees.txtgit write-tree builds a tree object from the current index, writes it, and prints its ID. The tree lists one entry: the blob, under the name fees.txt.
Step 4: Make a Commit
git commit-tree needs a tree and a message. A first commit has no parent:
tree=$(git write-tree)
first=$(git commit-tree "$tree" -m "Add the term fee")
echo "$first"
git cat-file -p "$first"e208b8e745c79e131aaf7cfb051448fe6fdef823
tree c2d44c59023fef48017eb9c7ffa24fed5b3e13a6
author Navika Anamaneni <navika@example.com> 1767629760 -0600
committer Navika Anamaneni <navika@example.com> 1767629760 -0600
Add the term feeExplanation: the commit object exists, with the tree, the author and committer from the configuration and environment, and the message. But no branch points to it yet:
git log --oneline 2>&1 | head -1fatal: your current branch 'main' does not have any commits yetStep 5: Move the Branch
git update-ref refs/heads/main e208b8e745c79e131aaf7cfb051448fe6fdef823
git log --oneline
git status -se208b8e (HEAD -> main) Add the term feeNavika copied the commit ID that step 4 printed, just as step 2 used the blob ID from step 1. Now main points to it, HEAD (which names main) has a commit, git log shows it, and the status is clean. That is a complete commit, made without git add or git commit.
A Second Commit, with a Parent
A second commit needs -p to name its parent. This time, every step's result goes straight into the next:
echo 'Late fee: $250' >> fees.txt
blob=$(git hash-object -w fees.txt)
git update-index --cacheinfo 100644,"$blob",fees.txt
tree=$(git write-tree)
commit=$(git commit-tree "$tree" -p HEAD -m "Add the late fee")
git update-ref refs/heads/main "$commit"
git log --oneline
git show --stat --format=%s HEAD40c3633 (HEAD -> main) Add the late fee
e208b8e Add the term fee
Add the late fee
fees.txt | 1 +
1 file changed, 1 insertion(+)-p HEAD made the old commit the parent, so git log shows a history of two commits, and git show computes the diff between them, exactly as for any commit.
Walking and Checking History: rev-list and fsck
Two more plumbing commands complete the picture. git rev-list walks history and prints commit IDs, the engine behind git log. git fsck checks that every object is intact and every link points to an existing object:
git rev-list HEAD
git rev-list --count HEAD
git fsck40c3633cb38c9d200cbd167b05a4ec2a02086a25
e208b8e745c79e131aaf7cfb051448fe6fdef823
2git rev-list takes the same ranges and filters as git log (main..feature, --since, -- <path>), but only prints IDs, which makes it the right tool in scripts. git fsck printing nothing (or only "dangling" notes) means the repository is consistent.
| Command | Description |
|---|---|
git rev-list <range> | It is used to list commit IDs, newest first. |
git rev-list --count <range> | It is used to count commits. |
git rev-list --objects --all | It is used to list every reachable object, with paths for blobs and trees. |
git fsck | It is used to verify object integrity and connectivity. |
Common Mistakes
Forgetting -w with hash-object. Without it, the ID is only calculated, nothing is stored, and later steps fail with "invalid object".
Forgetting -p in commit-tree. The new commit has no parent, so it starts a separate history that doesn't contain your earlier commits.
Writing the branch file directly. git update-ref locks the ref and writes the reflog. Writing .git/refs/heads/main yourself skips both.
Expecting the working tree to change. Plumbing commands work on objects, the index and refs. Your files on disk are only read, never written.
Using these steps for everyday work. They teach how Git works and help in scripts. For normal commits, git add and git commit add useful checks, hooks and messages.
Under the Hood
git commit performs these steps, plus some extras: it runs hooks (pre-commit, commit-msg), opens the editor for the message, checks that something changed, uses the current branch's commit as the parent automatically, and writes a reflog entry with a readable description. git commit-tree reads the author and committer from the same places as git commit: the GIT_AUTHOR_* and GIT_COMMITTER_* environment variables if set, otherwise user.name and user.email, with the current time. That is how the commits on these pages have reproducible IDs: the checker sets those variables to fixed values.
Interview Corner
How do you create a commit without git commit?
Store the content with git hash-object -w, stage it with git update-index --add --cacheinfo, create a tree with git write-tree, create the commit with git commit-tree <tree> -p <parent> -m "<message>", and point the branch at it with git update-ref refs/heads/<branch> <commit>.
What does git write-tree do?
It creates tree objects (one per folder) from the current index, writes them to the database, and prints the top-level tree's ID.
What is git rev-list?
The plumbing command that walks the commit graph and prints commit IDs for a range, with the same filters as git log. It is used in scripts, for example git rev-list --count HEAD.
What does git fsck check?
That every object is valid (its content matches its ID) and every reference and link points to an existing object. It also reports dangling (unreachable) objects.
Practice
Challenge: a branch made by hand. Create a branch feature/hostel with a commit that adds Hostel fee: $9,000 to fees.txt, using only plumbing, on top of main. Don't switch branches. Then show the graph of both branches.
What does Git print? Before you open the solution, decide whether main and the files on disk change.
Show solution
blob=$(printf 'Term 1 fee: $12,000\nLate fee: $250\nHostel fee: $9,000\n' | git hash-object -w --stdin)
git update-index --cacheinfo 100644,"$blob",fees.txt
tree=$(git write-tree)
git update-index --cacheinfo 100644,$(git rev-parse HEAD:fees.txt),fees.txt
commit=$(git commit-tree "$tree" -p main -m "Add the hostel fee")
git update-ref refs/heads/feature/hostel "$commit"
git log --oneline --graph --all
cat fees.txt
git status -s* db97ba7 (feature/hostel) Add the hostel fee
* 40c3633 (HEAD -> main) Add the late fee
* e208b8e Add the term fee
Term 1 fee: $12,000
Late fee: $250The new branch has a commit on top of main, while main, the files on disk and the status are unchanged. The blob was made from content piped in with --stdin, the index was put back to HEAD's version after writing the tree, and no branch was ever checked out.
Checkpoint. In an empty practice repository, create a two-file commit using only hash-object, update-index, write-tree, commit-tree and update-ref. Then run git log --stat and git fsck to confirm it is a normal, valid commit.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git hash-object -w <file> | Store content as a blob |
git update-index --add --cacheinfo <mode>,<id>,<path> | Stage a blob under a path |
git write-tree | Create a tree from the index |
git commit-tree <tree> -p <parent> -m "<msg>" | Create a commit |
git update-ref refs/heads/<branch> <commit> | Point a branch at a commit |
git rev-list --count HEAD | Count commits |
git fsck | Check the repository |
Was this tutorial helpful?