Another way is possible if you're using tree 1.8.0 sincetree 1.8.0
since it supports supports the --fromfile flag:
--fromfile Reads a directory listing from a file rather than the file-system. Paths provided on the command
line are files to read from rather than directories to search. The dot (.) directory indicates that tree
should read paths from standard input.
--fromfileReads a directory listing from a file rather than the file-system. Paths provided on the command line are files to read from rather than directories to search. The dot (
.) directory indicates that tree should read paths from standard input.
We could use git ls-tree to get all non-git-ignored files in a project, and pipe the output to tree.
Assuming we have a git repository, where ignored files are ignored in .gitignore:
git_repo
├── .gitignore
├── bar
│ ├── b.txt
│ └── ignored
├── foo
│ ├── a.txt
│ └── ignored
└── ignored
The following command:
git ls-tree -r --name-only HEAD | tree --fromfile
Gives:
.
├── .gitignore
├── bar
│ └── b.txt
└── foo
└── a.txt
2 directories, 3 files
Or if you need a specific path:
git ls-tree -r --name-only HEAD foo | tree --fromfile
Gives:
.
└── a.txt
0 directories, 1 file
Caveats
- Beware that changes such as deleted or renamed files that haven't already been committed can cause
git ls-treeto appear out of sync.