ls-files Git Command Guide
The git ls-files command shows information about files in the index (staging area) and the working tree. It provides detailed file state information, including cached, modified, untracked, and ignored files, with filtering options.
git ls-files Syntax:
Section titled “git ls-files Syntax:”git ls-files [-z] [-t] [-v] [-f] [-c|--cached] [-d|--deleted] [-o|--others] [-i|--ignored] [-s|--stage] [-u|--unmerged] [-k|--killed] [-m|--modified] [--resolve-undo] [--directory [--no-empty-directory]] [--eol] [--deduplicate] [-x <pattern>|--exclude=<pattern>] [-X <file>|--exclude-from=<file>] [--exclude-per-directory=<file>]<environment_details>File Selection Options:
Section titled “File Selection Options:”| Option | Description |
|---|---|
-c, --cached | Show cached files (tracked files in index) |
-d, --deleted | Show deleted files |
-o, --others | Show other (untracked) files |
-i, --ignored | Show ignored files only |
-s, --stage | Show staged contents hash and merge info |
-u, --unmerged | Show unmerged files |
-k, --killed | Show files killed in work tree |
-m, --modified | Show modified files |
--resolve-undo | Show resolve-undo information |
Output Control Options:
Section titled “Output Control Options:”| Option | Description |
|---|---|
-z | Use NUL line termination |
-t | Show file status |
-v | Show verbose output |
-f | Show FSMonitor valid bit |
--directory | Show directories instead of contents |
--no-empty-directory | Don’t show empty directories |
--eol | Show line ending information |
--deduplicate | Suppress duplicate entries |
Exclusion Options:
Section titled “Exclusion Options:”| Option | Description |
|---|---|
-x <pattern> | Skip files matching pattern |
-X <file> | Exclude patterns from file |
--exclude-per-directory=<file> | Read exclude file per directory |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
<file>... | Specific files to show information for |
Basic Usage Examples:
Section titled “Basic Usage Examples:”View Tracked Files:
Section titled “View Tracked Files:”# Show all tracked filesgit ls-files
# Show cached (staged) files onlygit ls-files --cached
# Show untracked filesgit ls-files --othersFile State Analysis:
Section titled “File State Analysis:”# Show modified files in working directorygit ls-files --modified
# Show unmerged files after merge conflictgit ls-files --unmerged
# Show deleted filesgit ls-files --deletedDetailed Information:
Section titled “Detailed Information:”# Show stage information (mode, object hash, stage number)git ls-files --stage
# Verbose status informationgit ls-files -t
# Show line ending informationgit ls-files --eolFiltering and Exclusion:
Section titled “Filtering and Exclusion:”# Exclude files matching patterngit ls-files -x "*.tmp"
# Exclude files in directorygit ls-files -x "build/"
# Use exclusion patterns from filegit ls-files -X .gitignoreAdvanced Usage Scenarios:
Section titled “Advanced Usage Scenarios:”Repository Analysis:
Section titled “Repository Analysis:”# Count total tracked filesgit ls-files | wc -l
# Find all files of specific typegit ls-files | grep "\.java$" | wc -l
# Check for large files in repositorygit ls-files -s | awk '$2 > 1048576 {print $4 ": " $2 " bytes"}' | sort -k2 -rnBackup and Synchronization:
Section titled “Backup and Synchronization:”# Create backup manifestgit ls-files > backup-manifest.txt
# Verify backup contentsgit ls-files | xargs -I {} diff {} /backup/{} 2>/dev/null || echo "File {} differs"CI/CD Integration:
Section titled “CI/CD Integration:”# Ensure no binary files in sourceif git ls-files | xargs file | grep -E ":[[:space:]]*[A-Z][a-z]* binary"; then echo "Binary files found in repository" exit 1fi
# Check for files larger than thresholdgit ls-files -s | awk '$2 > 10485760 {print "Large file: " $4 " (" $2 " bytes)"}'Merge Conflict Handling:
Section titled “Merge Conflict Handling:”# Show all conflicted filesgit ls-files --unmerged
# Detailed merge informationgit ls-files --stage | grep "^100644\|120000\|^100755"Directory Structure Analysis:
Section titled “Directory Structure Analysis:”# Show directory structure (without contents)git ls-files --directory
# Find empty tracked directoriesgit ls-files --directory | while read dir; do if [ -z "$(find "$dir" -maxdepth 1 -type f)" ]; then echo "Empty directory: $dir" fidoneExclusion Pattern Analysis:
Section titled “Exclusion Pattern Analysis:”# Show all ignored filesgit ls-files --ignored --others
# Verify .gitignore effectivenessgit ls-files --ignored --exclude-standard | head -20
# Show files that would be ignored but are trackedgit ls-files | while read file; do if git check-ignore -q "$file" 2>/dev/null; then echo "Should be ignored but tracked: $file" fidoneWorking Tree vs Index Analysis:
Section titled “Working Tree vs Index Analysis:”File State Comparison:
Section titled “File State Comparison:”# Files in index but not in working directorycomm -23 <(git ls-files) <(find . -type f -print | sed 's|^\./||')
# Files in working directory but not in indexcomm -13 <(git ls-files) <(find . -type f -print | sed 's|^\./||' | grep -v '^\.git')Untracked File Discovery:
Section titled “Untracked File Discovery:”# New untracked filesgit ls-files --others --exclude-standard
# All untracked files including ignoredgit ls-files --others
# New directoriesgit ls-files --others --directoryModification Tracking:
Section titled “Modification Tracking:”# Files with changesgit ls-files --modified
# Files staged for commitgit diff --cached --name-only | xargs git ls-files --stage
# Files that must be manually resolvedgit ls-files --unmerged | awk '{print $NF}' | sort | uniqPerformance and Efficiency:
Section titled “Performance and Efficiency:”Large Repository Handling:
Section titled “Large Repository Handling:”# Limit output for scriptinggit ls-files | head -1000
# Use NUL termination for filename safetygit ls-files -z | xargs -0 process-file
# Parallel processinggit ls-files | parallel -j4 'process-single-file {}'Caching and Optimization:
Section titled “Caching and Optimization:”# Use git status cache where possiblegit status --porcelain | awk '$1 !~ /^D/ {print $2}'
# Batch operations instead of per-file queriesgit ls-files -z | xargs -0 rm -f # Careful with this!Integration with Other Git Commands:
Section titled “Integration with Other Git Commands:”Complete Status Alternative:
Section titled “Complete Status Alternative:”#!/bin/bashecho "=== TRACKED FILES ==="git ls-files
echo "=== MODIFIED FILES ==="git ls-files --modified
echo "=== UNTRACKED FILES ==="git ls-files --others --exclude-standard
echo "=== IGNORED FILES ==="git ls-files --others --ignored --exclude-standardCleanup Script:
Section titled “Cleanup Script:”# Remove all ignored filesgit ls-files --ignored --others --exclude-standard -z | xargs -0 rm -f
# Archive tracked filesgit ls-files -z | xargs -0 tar czf archive.tar.gzRepository Statistics:
Section titled “Repository Statistics:”# File type distributiongit ls-files | xargs file -b | sort | uniq -c | sort -rn | head -20
# Biggest files by linesgit ls-files | xargs -I {} sh -c 'echo "$(wc -l < "{}") {}"' | sort -rn | head -10
# Oldest modified filesgit ls-files --modified -z | xargs -0 ls -lt | tail -10Troubleshooting Common Issues:
Section titled “Troubleshooting Common Issues:”File Not Showing:
Section titled “File Not Showing:”# Check if file existsls -la problematic-file
# Check if in indexgit ls-files | grep problematic-file
# Check if ignoredgit check-ignore -v problematic-file
# Check untracked filesgit ls-files --others | grep problematic-filePerformance Problems:
Section titled “Performance Problems:”# Disable expensive checksgit ls-files --no-empty-directory
# Use caching commandsgit status --porcelain # Faster overview
# Limit directory depthgit ls-files --directory | head -100Encoding Issues:
Section titled “Encoding Issues:”# Handle filename encodinggit ls-files | iconv -f utf-8 -t utf-8
# List files with special charactersgit ls-files -z | xargs -0 -I {} echo "'{}'"Permission Issues:
Section titled “Permission Issues:”# Check readable files onlygit ls-files 2>/dev/null
# Find files with permission issuesgit ls-files | xargs ls -l 2>&1 | grep -v "^-" | head -10What’s the difference between git ls-files and find?
Section titled “What’s the difference between git ls-files and find?”git ls-files shows repository-tracked files from index; find shows all filesystem files. ls-files respects .gitignore and Git’s view of repository.
How do I see all files including untracked?
Section titled “How do I see all files including untracked?”Combine options: git ls-files —others (untracked) + git ls-files (tracked). For all files including ignored: git ls-files —others.
What’s the —stage information?
Section titled “What’s the —stage information?”Shows mode (permissions), SHA-1 hash, stage number (for merges), and filename. Stage numbers: 0=normal, 1=ancestor, 2=ours, 3=theirs.
How do I verify repository health?
Section titled “How do I verify repository health?”git ls-files —unmerged shows unresolved conflicts. Missing expected files might indicate corruption. Compare against git status output.
Can ls-files show file sizes?
Section titled “Can ls-files show file sizes?”Indirectly with —stage: git ls-files —stage | while read mode hash stage file; do echo ”$(git cat-file -s $hash) $file”; done
How do exclusions work?
Section titled “How do exclusions work?”-x excludes patterns, -X excludes from file. Patterns use shell glob syntax: * ? [ ] etc. Directory exclusions end with /.
What’s the resolve-undo information?
Section titled “What’s the resolve-undo information?”Shows files that can be un-merged using git checkout -m (stage 0 objects from merge resolution that have been automatically saved).
Can ls-files detect file moves?
Section titled “Can ls-files detect file moves?”No, shows current index state. Use git log —follow —name-status to detect renames across history, or git status for recent changes.
How do I list files from a different branch?
Section titled “How do I list files from a different branch?”Check out branch first: git checkout other-branch && git ls-files. Or use git show :path for single file from specific commit.
What’s the difference between —cached and default?
Section titled “What’s the difference between —cached and default?”—cached explicitly shows only index contents. Default shows all tracked files (equivalent to —cached unless specifying other options).
Applications of the git ls-files command
Section titled “Applications of the git ls-files command”- Repository Auditing: Verify file inclusion/exclusion according to policies
- Backup Planning: Generate comprehensive file lists for archiving
- Performance Analysis: Identify large files impacting repository size
- CI/CD Validation: Ensure repository contains expected file types/locations
- Workflow Automation: Build scripts working with repository-tracked files
- Security Scanning: Apply security checks only to tracked source code