Skip to content
Latchkey

git symbolic-ref: Usage, Options & Common CI Errors

git symbolic-ref reads or updates symbolic references such as HEAD or refs/remotes/origin/HEAD.

symbolic-ref is the reliable way to discover the current branch or a remote’s default branch in scripts.

What it does

git symbolic-ref reads the target of a symbolic ref (e.g. HEAD points to refs/heads/main) or sets one. It is how you reliably query the current branch or origin’s default branch.

Common usage

Terminal
git symbolic-ref HEAD                 # refs/heads/main
git symbolic-ref --short HEAD         # main
git symbolic-ref refs/remotes/origin/HEAD   # origin's default
git symbolic-ref HEAD refs/heads/main # repoint HEAD

Options

FlagWhat it does
--shortStrip the refs/heads/ prefix
-d / --deleteDelete a symbolic ref
-q / --quietNo error if the ref is not symbolic
<name> <ref>Point a symbolic ref at a target

Common errors in CI

fatal: ref HEAD is not a symbolic ref - HEAD is detached (pointing at a SHA, common in CI). Use git rev-parse HEAD for the SHA instead. To find origin’s default branch when origin/HEAD is unset, run git remote set-head origin -a first.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →