Synopsis
git update-ref [-d] [--create-reflog] <ref> <newvalue> [<oldvalue>]
Description
The git update-ref command writes a new SHA to a ref atomically. With an optional expected old value, it provides compare-and-swap semantics for safety. It is what porcelain commands like git commit and git branch use under the hood, and what scripts that manipulate refs directly should use instead of editing files in .git/refs/.
In day-to-day use, git update-ref integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git update-ref --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git update-ref interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git update-ref's side effects helps avoid all three.
When to Use
Most developers never need this. Use it when writing automation that creates, moves, or deletes refs in custom namespaces (notes, replace, custom systems), or when a porcelain command isn't appropriate (e.g., creating a ref that points at an arbitrary tree).
Common Options
| Option | Description |
|---|---|
-d <ref> [<oldvalue>] | Delete a ref (optionally compare-and-swap). |
--no-deref | Don't follow symbolic refs. |
--create-reflog | Create a reflog if missing. |
-m <reason> | Reflog reason for this update. |
--stdin | Read updates from stdin (atomic batch). |
Examples
git update-ref refs/heads/feature abc123
# Move feature branch to commit abc123
git update-ref -d refs/heads/old-experiment
# Delete a branch ref directly
git update-ref refs/notes/ci HEAD def456
# Move a custom notes ref atomically
# Atomic multi-ref update via stdin:
printf 'update refs/heads/main %s\nupdate refs/heads/dev %s\n' "$NEW1" "$NEW2" | \
git update-ref --stdin
Common Mistakes
Bypassing safety by editing .git/refs/ files directly can corrupt packed refs or skip reflog entries. Always use update-ref. Forgetting to provide <oldvalue> defeats the compare-and-swap protection — provide it whenever you can.
Related Commands
git symbolic-ref, git pack-refs, git for-each-ref, git branch