Finding a file in Git

Find a file in working directory

# Find 'a.c' in the current directory and its subdirectories.
$ git ls-files -- '**/a.c' a.c
a.c
source/a.c

Find a file in staging area

# Find 'a.c' in the current directory of staging area
# --cached, show files in the staging area
$ git ls-files --cached -- '**/a.c' a.c

Find a file in a commit

It also shows whether a file is tracked or not in a commit.

# Find 'a.c' in the current directory of HEAD commit
# -r, recursely
# --name-only, list file names (including path) only
$ git ls-tree -r --name-only HEAD | grep a.c
a.c
source/a.c

Command git ls-tree -r --name-only <commit> lists all files in a specific commit.

Getting count of tracked files in Git

git ls-files lists files in index, combining it and wc -l which could count lines, you can get the count of tracked files in Git.

Get count of tracked files

To get count of tracked files, run below command:

# Get count of tracked files in the current directory, including files in its subdirectories
$ git ls-files | wc -l

git ls-files lists files in the index of the current folder, if a file is removed from the index, it won’t be listed. To get count of all tracked files in the repository, you need to navigate to the root directory of the repository.

git ls-files

git ls-files lists files in the index of the current folder:

# List tracked files
$ git ls-files
my-plugin.php
README.txt
assets/css/style.css
assets/js/my-plugin.js
...

wc

wc means word count, it can be used to count bytes, words, or lines. -l option is used to count lines.

Resources

Listing untracked files in Git

List all the untracked files, including ignored files

git ls-files command is used to list files, add -o or --others to list all the untracked files:

# List all the untracked files, including the ignored files
$ git ls-files --others

List untracked files, excluding ignored files

--exclude-standard option excludes files in .gitignore, .git/info/exclude or the user’s global exclusion file.

# List untracked files, excluding the ignored files.
$ git ls-files --others --exclude-standard