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
- First Example
- Issued
- git ls-files
- Output
- int/datatype.int.nullString.isDayNull.ps1
- Issued
- Second Example
- Issued
- git ls-files –full-name
- Output
- sanitize/null/datatype/int/datatype.int.nullString.isDayNull.ps1
- Issued
- Differences In Output
- First
- int/datatype.int.nullString.isDayNull.ps1
- Second
- sanitize/null/datatype/int/datatype.int.nullString.isDayNull.ps1
- First
- 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
- Our first example has the relative 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
- Issued
- git ls-files –error-unmatch ../../../README.md
- Intent
- Get README.md file three ( 3 ) folders up
- ../ ( each folder up )
- Get README.md file three ( 3 ) folders up
- Output
- ../../../README.md
- Environment Variable
- ERRORLEVEL
- ERRORLEVEL is zero ( 0 )
- ERRORLEVEL
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
- Issued
- git ls-files –error-unmatch ../../../README_NOT.md
- Intent
- Get README_NOT.md file three ( 3 ) folders up
- ../ ( each folder up )
- Get README_NOT.md file three ( 3 ) folders up
- Output
- error: pathspec ‘../../../README_NOT.md’ did not match any file(s) known to git
Did you forget to ‘git add’?
- error: pathspec ‘../../../README_NOT.md’ did not match any file(s) known to git
- Environment Variable
- ERRORLEVEL
- ERRORLEVEL is one ( 1 )
- ERRORLEVEL
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
- int/images/variable.string.is.null.01.20230317.1132AM.png
- 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
- Unfiltered
- Sample
- git ls-files
- Files from all folders
- int
- string
- Four ( 4 ) Files all together
- Sample
- Filtered – Folder – String
- Sample
- git ls-files –directory string
- Files from the string folder
- string
- Two ( 2 ) Files all together
- Sample