0

How do I use git ls-files to show files only(i.e., excluding directories)? For instance, I want to remove asd1 and asd2 from the following output:

$> git ls-files                         
asd1/bar
asd1/foo
asd2/bar
base.html
index.html
list.html

Is there any way to do this without involving string manipulation?

beep
  • 1,057
  • 2
  • 11
  • 23
  • Note that the above output, from `git ls-files`, contains only files. I think you were really trying to ask about `git ls-tree`, as shown in your own answer. – torek Mar 23 '21 at 00:01

2 Answers2

1

That's the kind of thing that pipes are used for:

 git ls-files | while read filename; do basename $filename; done

I could also go this route:

git ls-files | sed 's/.*\///'
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • This works partially: it still prints the the files inside `asd1`/`asd2`. i.e.: bar foo bar base.html index.html list.html – beep Mar 22 '21 at 22:49
0

Ok I finally got it working...I leave the answer hoping will be save some else time:

git ls-tree --name-only -r HEAD | grep -v -w -e "$(git ls-tree -d --name-only HEAD)"
beep
  • 1,057
  • 2
  • 11
  • 23
  • This uses `git ls-tree`, rather than `git ls-files`. They do different things! ls-tree examines a tree object; ls-files is complicated, but generally starts with the index. Git's index has the form of a "flattened tree" from which all directories have been removed. – torek Mar 22 '21 at 23:59