Synopsis
git ls-tree [-r] [-d] [-l] [--name-only] <tree-ish> [<path>...]
Description
The git ls-tree command lists entries (mode, type, SHA, name) of a tree object. With -r it recurses into subdirectories. It is the plumbing equivalent of ls on a Git tree. Output is one entry per line, suitable for piping into other tools.
In day-to-day use, git ls-tree 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 ls-tree --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 ls-tree 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 ls-tree's side effects helps avoid all three.
When to Use
Most developers reach for git show <rev>:<path> or git ls-files instead. ls-tree shines in scripts that need exact mode/type/SHA information, or when traversing a tree of any commit (not just HEAD's index).
Common Options
| Option | Description |
|---|---|
-r | Recurse into subtrees. |
-d | Show only directories (trees). |
-l | Include the size of blob entries. |
--name-only | Show only file names. |
-z | NUL-terminate output for safe parsing. |
--full-tree | Don't restrict by current directory. |
Examples
git ls-tree HEAD
# Top-level entries of HEAD's tree
git ls-tree -r HEAD --name-only
# All file paths at HEAD
git ls-tree -r -l v1.0 src/
# Sizes and SHAs for every file under src/ at v1.0
git ls-tree HEAD -- 'docs/*.md'
# Just markdown files in docs/
Common Mistakes
Mixing up working-directory-relative versus repo-root-relative paths. Use --full-tree for unambiguous root-relative output. Without -r, only one level is shown — easy to miss nested files.
Related Commands
git ls-files, git show, git cat-file, git diff-tree