how to `git ls-files` for just one directory level.

后端 未结 7 882
广开言路
广开言路 2020-12-31 00:55

I\'m using msysgit (1.7.9), and I\'m looking for the right invocation of the git ls-files command to show just the (tracked) files and directories at the curren

相关标签:
7条回答
  • 2020-12-31 01:37

    use this simple bash script for define subdirectory any level

    0 讨论(0)
  • 2020-12-31 01:38

    git ls-tree <tree-ish> is good and all, but I can't figure out how to specify the index as the <tree-ish>. (Although I'm sure there's bound to be some all-caps reference to do just that.)

    Anyhow, ls-files implicitly works on the index so I might as well use that:

    $ git ls-files | cut -d/ -f1 | uniq
    

    This shows files and directories only in the current directory.

    Change cut's -f argument to control depth. For instance, -f-2 (that's dash two) shows files and directories up to two levels deep:

    $ git ls-files | cut -d/ -f-2 | uniq
    

    IF you specify the <path> argument to ls-files, make sure to increase -f to accommodate the leading directories:

    $ git ls-files foo/bar | cut -d/ -f-3 | uniq
    
    0 讨论(0)
  • 2020-12-31 01:46

    I think you want git ls-tree HEAD sed'd to taste. The second word of ls-tree's output will be tree for directories, blob for files, commit for submodules, the filename is everything after the ascii tab.

    Edit: adapting from @iegik's comment and to better fit the question as asked,

    git ls-files . | sed s,/.*,/, | uniq
    

    will list the indexed files starting at the current level and collapse directories to their first component.

    0 讨论(0)
  • 2020-12-31 01:48

    I believe git ls-tree --name-only [branch] will do what you're looking for.

    0 讨论(0)
  • 2020-12-31 01:54

    To just list the files in the current working directory that are tracked by git, I found that the following is several times faster than using git ls-tree...:

    ls | grep -f <(git ls-files)
    

    It would take a little messing around with sed if you also wanted to include directories, something along the lines of:

    ls | grep -f <(git ls-files | sed 's/\/.*//g' | sort | uniq)  
    

    assuming you don't have any '/' characters in the names of your files. As well as...

    ls -a | grep -f <(git ls-files | sed 's/\/.*//g' | sort | uniq)

    in order to also list "invisible" (yet-tracked) files.

    0 讨论(0)
  • 2020-12-31 01:57

    I'm surprised this is so hard... but don't get me started on my griping about git.

    A variant on jthill's answer seems to be aliasable (hey, I'm a linguist, I have a license to make new words). The variant is

    ls -d `git ls-tree HEAD | sed -e "s/^.*\t//"`
    

    This uses 'ls' to format the output, so you get color coding (if you use that), etc. It also works as an alias:

    alias gitls='ls -d `git ls-tree HEAD | sed -e "s/^.*\t//"`'
    

    FWIW, you can also alias the recursive command, so that you used the 'ls' formatting (e.g. if your path+filenames aren't too long, you'll get two column output, color coding of executables, etc.)

    alias gitls-r='ls `git ls-files`'
    
    0 讨论(0)
提交回复
热议问题