Git:- List File ( git ls-files )

Background

Yesterday I was re-arranging some files and needed to double reaffirm that the files were properly moved.

Git

Git List Files

To list files git provides the git ls-files command.

 

Git ls-files

Option

Option Explanation Full Explanation
–full-name Display Full File name When run from a subdirectory, the command usually outputs paths relative to the current directory.
This option forces paths to be output relative to the project top directory.
–error-unmatch Raises an error upon missing requested file If any does not appear in the index, treat this as an error (return 1).
–others Display untracked files Show other (i.e. untracked) files in the output
–directory Only list files for specific folder

Syntax


git ls-files [filename] 

Sample

Basic

git ls-files

Output – Image

Output – Text

>git ls-files

int/datatype.int.nullString.isDayNull.ps1

Option –full-name

git ls-files --full-name

Output – Image

Output – Text

>git ls-files --full-name

sanitize/null/datatype/int/datatype.int.nullString.isDayNull.ps1
Explanation
  1. First Example
    • Issued
      • git ls-files
    • Output
      • int/datatype.int.nullString.isDayNull.ps1
  2. Second Example
    • Issued
      • git ls-files –full-name
    • Output
      • sanitize/null/datatype/int/datatype.int.nullString.isDayNull.ps1
  3. Differences In Output
    • First
      • int/datatype.int.nullString.isDayNull.ps1
    • Second
      • sanitize/null/datatype/int/datatype.int.nullString.isDayNull.ps1
  4. Noted
    • Our first example has the relative name of the file
      • The name is based on our current folder/directory
    • Our second example has the full name of the file

 

Option –error-unmatch ( File Present )
Outline

Normally, if git ls-files is issued against missing files, the OS Error Flag is not set.

To have the OS Error Flag set, please pass along the –error-unmatch operand.

Code

git ls-files --error-unmatch ../../../README.md

Output – Image

Output – Text

>git ls-files --error-unmatch ../../../README.md
../../../README.md

>echo ERRORLEVEL is %ERRORLEVEL%
ERRORLEVEL is 0

>

Explanation
  1. Issued
    • git ls-files –error-unmatch ../../../README.md
  2. Intent
    • Get README.md file three ( 3 ) folders up
      • ../ ( each folder up )
  3. Output
    • ../../../README.md
  4. Environment Variable
    • ERRORLEVEL
      • ERRORLEVEL is zero ( 0 )

 

 

Option –error-unmatch ( File Present NOT )
Outline

Normally, if git ls-files is issued against missing files, the OS Error Flag is not set.

To have the OS Error Flag set, please pass along the –error-unmatch operand.

Code

git ls-files --error-unmatch ../../../README_NOT.md

Output – Image

Output – Text
</pre>
>git ls-files --error-unmatch ../../../README_NOT.md
error: pathspec '../../../README_NOT.md' did not match any file(s) known to git
Did you forget to 'git add'?

>echo ERRORLEVEL is %ERRORLEVEL%
ERRORLEVEL is 1

>

Explanation
  1. Issued
    • git ls-files –error-unmatch ../../../README_NOT.md
  2. Intent
    • Get README_NOT.md file three ( 3 ) folders up
      • ../ ( each folder up )
  3. Output
    • error: pathspec ‘../../../README_NOT.md’ did not match any file(s) known to git
      Did you forget to ‘git add’?
  4. Environment Variable
    • ERRORLEVEL
      • ERRORLEVEL is one ( 1 )

 

Show Untracked files ( –others )

git ls-files --others

Output – Image

Output – Text

>git ls-files --others
int/images/variable.string.is.null.01.20230317.1132AM.png
string/images/variable.string.is.null.01.20230317.1132AM.png

Output – Explanation

Untracked files are listed.

In this case, files that were created for the sake of blogging were shown.

Example

  1. int/images/variable.string.is.null.01.20230317.1132AM.png
  2. string/images/variable.string.is.null.01.20230317.1132AM.png

 

List files for specific folders ( –directory )
Unfiltered

git ls-files

Output – Image

Output – Text

>git ls-files
int/datatype.int.nullString.isDayNull.ps1
int/datatype.int.nullable.isDayNull.ps1
string/datatype.string.isDayNull.ps1
string/datatype.string.nullString.isDayNull.ps1

 

Filtered – Folder – String

git ls-files --directory string

Output – Image
Output – Text

>git ls-files --directory string
string/datatype.string.isDayNull.ps1
string/datatype.string.nullString.isDayNull.ps1

>
Explanation
  1. Unfiltered
    • Sample
      • git ls-files
    • Files from all folders
      • int
      • string
    • Four ( 4 ) Files all together
  2. Filtered – Folder – String
    • Sample
      • git ls-files –directory string
    • Files from the string folder
      • string
    • Two ( 2 ) Files all together

References

  1. git-scm
  2. UBUNTU
    • git-ls-files – Show information about files in the index and the working tree
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s