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

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 current level, either from the index, or the current working directory if that’s easier.

Essentially it would give a directory listing similar that that you would see on Github. Coming from Windows, I’m not too familiar with the right way of doing the globbing(?).

  • How to undo the initial commit on a remote repository in git?
  • Joomla Custom Compoment Dev Env : Hot Deployment
  • Trying to use Git with Android Studio
  • How can I copy a directory from one git repository to another, preserving history?
  • How can I get the latest commit ID of the currently checked out branch in git, using ANT, without having an active git installation?
  • git thinks files have changed
  • How can I discard modified files?
  • git: have different .gitignore file for each remote
  • Setting up sublimetext 3 as git commit text editor
  • Create a directory in Gitlab repo through web interface
  • Phabricator Key Words in Git Commit Messages
  • How to change the current branch to master in Egit?
  • 4 Solutions collect form web for “how to `git ls-files` for just one directory level.”

    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 submodulesm, the filename is everything after the ascii tab

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

    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.

    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
    Git Baby is a git and github fan, let's start git clone.