linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Tools/rev-parse

kb/tools ── Tools ── intermediate

git rev-parse

A plumbing command that translates human-readable ref names (HEAD, main~2, v1.0, :/typo) into full SHAs. Used by all porcelain commands under the hood, and convenient in scripts.

view as markdownaka: git-rev-parse

git rev-parse is Git's dictionary. It understands every form of commit and ref notation, and converts them to a single canonical SHA.

Basic form

bash
git rev-parse HEAD
# a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
git rev-parse main
# 7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d

If the name is ambiguous, it returns an error. If the name does not exist, the exit code is non-zero.

Ref syntax

NotationMeaning
HEADwhere HEAD currently points
mainthe tip of branch main
v1.0the commit the tag points to
HEAD~3three commits back along the first parent
HEAD^first parent (same as HEAD~1)
HEAD^2second parent (for merge commits)
HEAD@{yesterday}where HEAD was yesterday (via reflog)
:/fix typothe most recent commit whose message contains "fix typo"
<tag>^{commit}the commit that an annotated tag points to
HEAD:src/main.rsthe blob of this file from the current commit

Utility modes

Beyond translating names to SHAs, rev-parse has several practical modes:

bash
git rev-parse --show-toplevel
# /home/user/project           <- repository root
git rev-parse --git-dir
# /home/user/project/.git      <- where .git/ lives
git rev-parse --abbrev-ref HEAD
# main                          <- current branch name
git rev-parse --short HEAD
# a1b2c3d                       <- short unique SHA
git rev-parse --is-inside-work-tree
# true                          <- are we inside a repository?

--show-toplevel is especially useful in scripts and git hooks: it returns the repository root from any subdirectory.

In scripts

Capture the current commit SHA:

bash
CURRENT=$(git rev-parse HEAD)

Check that you are inside a repository:

bash
if git rev-parse --git-dir > /dev/null 2>&1; then
  echo "this is a git repo"
fi

Get the current branch name (without refs/heads/):

bash
BRANCH=$(git rev-parse --abbrev-ref HEAD)

These are standard patterns in bash scripts that work with Git.

Pitfalls

  • HEAD^ and HEAD~1 are the same thing. But HEAD^2 is not the same as HEAD~2: the first is the second parent of a merge commit; the second is two commits back along the first parent.
  • git rev-parse <tag> on an annotated tag returns the SHA of the tag object, not the commit. If you need the commit, use <tag>^{commit}.
  • On an empty repository, git rev-parse HEAD returns an error because HEAD does not point to anything yet.

§ команды

bash
git rev-parse HEAD

SHA of the current commit

bash
git rev-parse --abbrev-ref HEAD

Name of the current branch

bash
git rev-parse --show-toplevel

Path to the repository root

bash
git rev-parse HEAD:src/main.rs

SHA of the blob for this file in the current commit

§ см. также

  • cat-filegit cat-fileA plumbing command for reading objects in `.git/objects/` by their SHA. The main flags are: `-t` (type), `-s` (size), `-p` (content in human-readable form), `-e` (check whether the object exists).
  • loggit logTraverses the commit graph and prints each commit. By default it starts at HEAD and follows parents. A dozen flags cover 95% of history-browsing scenarios.
  • branchBranchA branch in Git is a file inside `.git/refs/heads/` that holds the SHA of one commit. Creating, switching, and deleting branches are trivial operations because a branch contains exactly 41 bytes of data.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies