'git ls-tree output of working directory?
I am looking for a way to have output in the same manner as ls-tree, but of my working directory. Whenever I run git ls-tree . it says fatal: Not a valid object name .
Solution 1:[1]
This is similar to @mikel's answer, but uses git stash create and ls-tree, as requested by the OP.
Also avoids using git reset, which is more likely to break things for the inexperienced user.
This however only works for tracked files.
git ls-tree `git diff --quiet && echo HEAD || git stash create ls-tree`
This will leave a dangling commit, which will should eventually be removed by git gc.
(Actually two dangling commits.)
Of course you could search for dangling commits containing ls-tree, but I haven't found a simple way to do so (at least not without quite a bit of sed and grep magic - suggestions welcome ).
Explanation
git ls-tree needs a hash. If the tree is clean (git diff --quiet returns 0) one can use HEAD. If it isn't, git stash create will create a commit and return it's hash.
Untracked
Unfortunately git stash create does not support -a/-u or other flags. Thus it's not possible to show the hashes of untracked files. Getting their information is a bit more complicated:
git stash -a
git ls-tree stash
git ls-tree stash^3
git stash pop
This will first show tracked files (git ls-tree stash) and then untracked files (git ls-tree stash^3).
torek provides a good explanation why stash^3 is needed.
Solution 2:[2]
Tried to find something which is not touching the git repo.
This one is not git only and depends on linux like 'grep'
#from root of repo dir
git ls-tree -r HEAD
#other dirs
git ls-tree -r HEAD | grep <relative_path_to_repo_root>/
# eg
git ls-tree -r HEAD | grep src/
Also found following (git only) working in subdirectories of repo root
git ls-tree -r --full-name HEAD
However man page (man git-ls-tree) is irritating git logic
--full-name
Instead of showing the path names relative to the current working directory, show the full path names.
--full-tree
Do not limit the listing to the current working directory. Implies --full-name.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
