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 .
git ls-tree output of working directory?
34.8k Views Asked by Alexander Bird AtThere are 3 best solutions below
On
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.
On
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.
git ls-treeonly works with git refs, e.g.ls-tree HEADorls-tree 1.9.1Try
git ls-files. You probably want the-sand/or-mflags.As you point out,
git ls-files -swill list the files in the index (i.e. files that have been staged).In theory, you could mess with the index, run
git ls-files -s, then try restore it, e.g.Seems right, and worked in a simple test, but could eat all your files.