Progress:
TIER 3 · MODULE 39· Expert

git rev-parse

Turn HEAD~3 / origin/main / :/typo / etc. into a SHA.

🎯 What & why

git rev-parse turns any revision spec - HEAD~3, origin/main, :/typo, v1.0^{commit} - into a concrete SHA. It also exposes repo layout facts.

🧠 Mental model

Two jobs in one binary: (1) resolve names to OIDs, (2) answer 'where am I?' questions about the current repo. Every shell-script-flavored Git tool calls it.

🛠️ Synopsis

git rev-parse [<options>] <args>...

  git rev-parse HEAD                  # full SHA of HEAD
  git rev-parse --short=8 HEAD        # 8-char abbreviation
  git rev-parse --abbrev-ref HEAD     # 'main' instead of refs/heads/main
  git rev-parse --show-toplevel       # absolute path to repo root
  git rev-parse --is-inside-work-tree # 'true' or 'false'

🎚️ Switches & options

FlagWhat it does
<args>Revision specs or option flags to resolve
--verifyRequire args to resolve to exactly one object; exit nonzero otherwise
--short[=<n>]Abbreviate output SHAs to the shortest unique prefix (or n chars)
--symbolicPrint the input symbolic name when it is one
--symbolic-full-nameExpand to full ref name (refs/heads/main)
--abbrev-ref[=<mode>]Shorten to short ref name (main, origin/main)
--git-dirPrint the path to the .git directory
--show-toplevelPrint the absolute path to the working-tree root
--is-inside-work-treePrint true/false: are we inside a working tree
--is-bare-repositoryPrint true/false: is this a bare repo
--git-common-dirPrint the common .git dir (matters for worktrees)

💡 Use cases

🧪 Examples

Full SHA of a ref
git rev-parse HEAD
Validate a ref exists
git rev-parse --verify --quiet refs/heads/feature
Find repo root in scripts
ROOT=$(git rev-parse --show-toplevel)
Current branch name
git rev-parse --abbrev-ref HEAD

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.