git ls-tree output of working directory?

后端 未结 2 1382
醉话见心
醉话见心 2021-02-02 16:15

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

相关标签:
2条回答
  • 2021-02-02 16:34

    git ls-tree only works with git refs, e.g. ls-tree HEAD.

    Try git ls-files. You probably want the -s and/or -m flags.


    As you point out, git ls-files -s will 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.

    git commit
    git add .
    git ls-files -s
    git reset .
    git reset --soft HEAD^
    

    Seems right, and worked in a simple test, but could eat all your files.

    0 讨论(0)
  • 2021-02-02 16:34

    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.

    0 讨论(0)
提交回复
热议问题