Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all files tracked by Git excluding submodules?

Tags:

git

bash

git ls-files

also lists submodules.

With: List submodules in a git repository and How to remove the lines which appear on file B from another file A? I can do:

git ls-files | grep -Fxvf <(git submodule status | cut -d' ' -f3)

or the more verbose and versatile:

git ls-files | while IFS='' read -r file; do
  if [ -f "$file" ]; then
    echo "$file"
  fi
done

Is there a shorter way using some git command / flags?


2 Answers

git grep --cached -l ''

lists all non-empty files, excluding both submodules and symlinks.

You may also be interested in excluding binary files for your mass refactorings: How to list all text (non-binary) files in a git repository?

Tested on Git 2.16.1 with this test repo.


I would strongly recommend parsing the .gitmodules file directly is generally safer and much faster than git submodule status. This file can be parsed using git config to ensure that all parsing edge cases are handled appropriately -- formatting, whitespace, comments, etc:

git ls-files | grep -Fxvf  <(git config --file .gitmodules --name-only --get-regexp path | cut -d '.' -f2-)

Alternatively, the file can be parsed directly so long as the regex is designed so that it won't match submodules which have been commended out via #:

git ls-files | grep -Fxvf  <(grep "^\s*\[submodule " .gitmodules | cut -d '"' -f2)

Note that git submodule status has an output format that makes it difficult to correctly to parse via cut. The two main drawbacks are:

  • the field of the filename will change depending on if the module is initialized or not

  • the filename is not delimited by tab \t, and it isn't the last thing on the line, so filenames with spaces cannot be handled correctly.

like image 35
jstine Avatar answered Sep 21 '22 03:09

jstine