Sometimes you want to know what a branch contains without switching to it. Checking it out just to run ls is slow and messes with your working folder.
git ls-tree -r --name-only main
It reads the commit’s tree straight from the repository. -r goes into subfolders, --name-only drops the modes and hashes. Instead of a branch you can give a tag, a hash or HEAD~5. In a throwaway repository with those three files committed, it prints:
.gitignore
README.md
src/app.py
Without -r you only get the top level, and subfolders show as tree entries. Add -l to see the size of each file next to its name:
git ls-tree -l HEAD
Pair it with git show main:src/app.py to read a file from that commit.
For scripts, -z ends each entry with a NUL byte and stops Git from quoting unusual file names, as the documentation describes. With -r, adding -t also lists the tree entries themselves while it recurses.
Caveat: it lists what was committed, not what’s on disk, so it knows nothing about untracked or ignored files. Run from a subfolder, it lists only that folder and prints paths relative to it (app.py instead of src/app.py). Add --full-tree to see everything from the root.
Further reading: git-ls-tree documentation