brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git ls-tree -r --name-only lists the files in any commit without checking it out

Git
~/tips/tip-git-ls-tree-name-only

git ls-tree -r --name-only main

Lists every file in a commit or branch as plain paths, without touching your working folder.

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

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link