Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

ls

glob

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(?).

like image 381
Philip Oakley Avatar asked May 04 '12 16:05

Philip Oakley


People also ask

How do I use the ls command in git?

Use the terminal to display the . git directory with the command ls -a . The ls command lists the current directory contents and by default will not show hidden files. If you pass it the -a flag, it will display hidden files.

How do I select a directory in git bash?

Go to the directory manually and do right click → Select 'Git bash' option. Git bash terminal automatically opens with the intended directory. For example, go to your project folder. While in the folder, right click and select the option and 'Git bash'.

How do I navigate folders in git?

To change this current working directory, you can use the "cd" command (where "cd" stands for "change directory"). For example, to move one directory upwards (into the current folder's parent folder), you can just call: $ cd ..


2 Answers

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.

like image 166
jthill Avatar answered Sep 27 '22 22:09

jthill


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

like image 26
David Cain Avatar answered Sep 27 '22 22:09

David Cain