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

后端 未结 7 909
广开言路
广开言路 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:38

    git ls-tree is good and all, but I can't figure out how to specify the index as the . (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 argument to ls-files, make sure to increase -f to accommodate the leading directories:

    $ git ls-files foo/bar | cut -d/ -f-3 | uniq
    

提交回复
热议问题