Refs, HEAD and Plumbing Commands: Building a Commit by Hand
How names point into the object database: branches, tags and remote-tracking refs as files holding IDs, HEAD as a symbolic ref, detached HEAD, packed-refs, special refs like ORIG_HEAD and FETCH_HEAD, and the plumbing commands that build a commit by hand, from hash-object to update-ref.
kvi5hnuCode Sprout team member7 min readOn this page
Objects (article 48) are identified by hashes, which nobody wants to type. References, or refs, are the human-friendly names on top: a branch, a tag or a remote-tracking branch is a name that holds one object ID. HEAD says which branch you're on. And every porcelain command you've used, such as commit, branch, reset or merge, comes down to creating objects and moving refs. This article shows how refs are stored, then builds commits with plumbing commands alone, to show exactly what git commit does.
What a ref is
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
git branch maps
git tag v1.0
git tag -a v1.1 -m "Version 1.1"
echo "=== HEAD"; cat .git/HEAD
echo "=== main"; cat .git/refs/heads/main
git for-each-ref --format='%(refname) %(objecttype) %(objectname:short)'
echo "=== detached HEAD"
git switch -q --detach
cat .git/HEAD
git symbolic-ref -q HEAD || echo "HEAD is not a symbolic ref"
=== HEAD
ref: refs/heads/main
=== main
65a52653961b94871312bc00033b060c13a5c274
refs/heads/main commit 65a5265
refs/heads/maps commit 65a5265
refs/tags/v1.0 commit 65a5265
refs/tags/v1.1 tag 77f8f2f
=== detached HEAD
65a52653961b94871312bc00033b060c13a5c274
HEAD is not a symbolic ref
- A branch is
refs/heads/<name>, holding a commit ID. A tag isrefs/tags/<name>: a lightweight tag holds the commit ID directly, while an annotated tag holds the ID of a tag object, which points to the commit. Remote-tracking branches live inrefs/remotes/<remote>/<name>, notes inrefs/notes/, and the stash inrefs/stash. HEADis normally a symbolic ref: it containsref: refs/heads/main, a pointer to a branch rather than to a commit.git symbolic-ref HEADreads it.- In detached HEAD (article 24),
HEADholds a commit ID directly. New commits then move onlyHEAD, and no branch follows them. git for-each-ref(orgit refs list, Git 2.52) lists refs with custom formats, andgit show-reflists them with IDs.
With the default files storage, each ref may be a small file under .git/refs/, or a line in .git/packed-refs, where Git collects refs to save space: git pack-refs --all moves them there, and git gc does it automatically. So don't read or edit files in .git/refs directly in scripts: use git rev-parse, git for-each-ref and git update-ref, which handle both, and also the newer reftable format (article 50).
How a name becomes an ID
When you type main, Git searches these locations in order and uses the first that exists:
.git/<name>, forHEAD,ORIG_HEADand similarrefs/<name>refs/tags/<name>refs/heads/<name>refs/remotes/<name>refs/remotes/<name>/HEAD
That's why a tag and a branch with the same name are ambiguous, with the tag winning and Git warning "refname is ambiguous". Use full names such as refs/heads/v1.0 to be explicit. git rev-parse --symbolic-full-name main shows the full name of an unambiguous name. For an ambiguous one it fails, and git for-each-ref 'refs/*/v2' lists every candidate.
Special refs
| Ref | Set by | Holds |
|---|---|---|
ORIG_HEAD | reset, rebase, merge, am | where HEAD was before the operation, for undoing it (article 24) |
FETCH_HEAD | fetch, pull | what was just fetched, read by pull to merge it |
MERGE_HEAD | a merge that stopped | the commit being merged in |
CHERRY_PICK_HEAD, REVERT_HEAD | a cherry-pick or revert that stopped | the commit being applied |
REBASE_HEAD | a rebase that stopped | the commit currently being replayed |
Building a commit by hand
Here is git add and git commit done with plumbing commands, one step at a time:
git init -q trail-guide && cd trail-guide
blob=$(echo "Lake loop: 5 km" | git hash-object -w --stdin) # 1. store the content
git update-index --add --cacheinfo 100644,$blob,lake.txt # 2. stage it under a name
tree=$(git write-tree) # 3. write the index as a tree
first=$(echo "Add lake page" | git commit-tree $tree) # 4. create the commit
git update-ref -m "commit (initial): by hand" refs/heads/main $first # 5. move the branch
echo "first commit: $first"
blob2=$(echo "Ridge loop: 8 km" | git hash-object -w --stdin)
git update-index --add --cacheinfo 100644,$blob2,ridge.txt
second=$(echo "Add ridge page" | git commit-tree $(git write-tree) -p $first)
git update-ref -m "commit: by hand" refs/heads/main $second $first # only if main is still $first
git log --oneline --stat
git reflog main
echo "=== the working tree was never touched"
git status --short
first commit: 65a52653961b94871312bc00033b060c13a5c274
2ac6670 Add ridge page
ridge.txt | 1 +
1 file changed, 1 insertion(+)
65a5265 Add lake page
lake.txt | 1 +
1 file changed, 1 insertion(+)
2ac6670 main@{0}: commit: by hand
65a5265 main@{1}: commit (initial): by hand
=== the working tree was never touched
D lake.txt
D ridge.txt
The five steps are what porcelain does for you:
git hash-object -wstores a blob, like the first half ofgit add.git update-index --add --cacheinfo <mode>,<id>,<path>records it in the index under a name, like the second half.git write-treeturns the index into tree objects and prints the top tree's ID.git commit-tree <tree> [-p <parent>]creates a commit object, with the message from standard input. Author and committer come from your configuration and environment.git update-refmoves the branch to the new commit, and writes a reflog entry with-m. Given an old value as the last argument, it updates only if the ref still has that value, a safe compare-and-swap for scripts.
Since HEAD points to refs/heads/main, moving main moved HEAD too. The files were never written to the working tree, so git status reports them as deleted: the index and commits have them, the folder doesn't. git restore . or git checkout -- . would write them out.
The first commit's ID, 65a5265, matches the "Add lake page" commit made with git add and git commit in the first example: same content, same tree, same author and date, so the same ID.
Porcelain as objects plus refs
| Porcelain command | In terms of objects and refs |
|---|---|
git commit | write blobs, trees and a commit, then move the current branch |
git branch topic | create refs/heads/topic holding HEAD's ID |
git switch topic | point HEAD to refs/heads/topic, and update the index and files |
git tag -a v2 | write a tag object, and create refs/tags/v2 holding its ID |
git reset --soft X | move the current branch to X |
git fetch | download objects, then update refs/remotes/... |
git merge (fast-forward) | move the current branch forward |
git branch -d topic | delete the ref. The commits remain until garbage collection |
That last row explains recovery (article 26): deleting a branch or resetting removes a name, not the objects. The reflog records where refs pointed, so the commits can be found again.
Plumbing for scripts
Plumbing commands have stable output meant for scripts, while porcelain output can change between versions. Useful ones:
| Command | Use |
|---|---|
git rev-parse <rev> | resolve any name to an ID. --verify -q to test it exists |
git rev-parse --abbrev-ref HEAD | the current branch name |
git symbolic-ref --short HEAD | the same, failing in detached HEAD |
git for-each-ref --format=... | list refs with any fields |
git refs exists <ref> | test that a ref exists (Git 2.52). Also git show-ref --exists |
git update-ref [-d] <ref> <new> [<old>] | create, move or delete a ref safely |
git rev-list <range> | list commit IDs |
git cat-file --batch | read many objects quickly |
git status --porcelain=v2 | machine-readable status |
What's current
git refsgroups ref-management subcommands:migrateto convert between storage formats,verify, since Git 2.52listandexists, and since Git 2.56create,delete,updateandrename.- The reftable ref format, an alternative to loose files and
packed-refs, is available today and planned as the default in Git 3.0 (article 50). Scripts that use plumbing commands work with either. git rev-parse --show-ref-formatreports which ref format a repository uses.
Common questions
What is HEAD, exactly?
A reference to what you have checked out: usually a symbolic ref naming the current branch, or a commit ID directly in detached HEAD. Each worktree has its own HEAD.
Why can't I find my branch in .git/refs/heads?
It may have been moved into .git/packed-refs, or the repository may use the reftable format. Use git show-ref or git for-each-ref instead of reading files.
What's the difference between git rev-parse --abbrev-ref HEAD and git branch --show-current?
Both print the current branch. In detached HEAD, rev-parse --abbrev-ref prints HEAD, and branch --show-current prints nothing.
Is it safe to use git update-ref instead of git reset?
Yes, for moving a ref that isn't checked out. If it's the current branch, update-ref moves it without touching the index or files, like reset --soft.
Summary
- Refs are names holding object IDs:
refs/heads/for branches,refs/tags/,refs/remotes/, plusrefs/stashandrefs/notes/. HEADis a symbolic ref to the current branch, or a commit ID when detached.ORIG_HEAD,FETCH_HEADandMERGE_HEADare temporary special refs.- Refs live in loose files, in
packed-refs, or in reftable, so use commands, not file paths. - A commit by hand:
hash-object -w,update-index --add --cacheinfo,write-tree,commit-tree,update-ref. - Every porcelain command is objects plus ref changes, which is why deleted branches can be recovered.
Practice questions
What does .git/HEAD contain on branch main, and after git switch --detach?
Answer
On main: ref: refs/heads/main, a symbolic ref. Detached: the 40-character ID of the commit directly. New commits in detached HEAD update only HEAD, which is why they can be lost when you switch away.
A script needs to set refs/heads/release to commit $new, but only if nobody else moved it from $old in the meantime. Which command?
Answer
git update-ref refs/heads/release $new $old. It fails if the ref doesn't currently hold $old, so concurrent changes aren't overwritten. Add -m "<reason>" to record a reflog message.
You have a tag and a branch both called v2. What does git log v2 show, and how do you pick the other one?
Answer
The tag, because refs/tags/ is searched before refs/heads/, and Git warns that the name is ambiguous. Use git log refs/heads/v2 for the branch, and rename one of them to avoid confusion.
Previous: Git Objects: Blobs, Trees, Commits and Tags
Next: How Git Stores Data: Packfiles, Garbage Collection, SHA-256, Reftable and Git 3.0

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.