Rabe
Rabe

Reputation: 333

How to retrieve all non deleted file form Repo using git

I am using git ls-files to list all the file indexed by git in a repository, however, the result shows some deleted files.

I am looking for a git command that list only non deleted file in the git repo.

Upvotes: 6

Views: 954

Answers (2)

Purkhalo Alex
Purkhalo Alex

Reputation: 3627

This script should print all modified and untracked files, then exclude removed if thus exist.

git ls-files -o -m --exclude-standard | grep -vE "^$(git ls-files -d | paste -sd "|" -)$"

Description:

  • git ls-files --other- shows other (i.e. untracked) files in the output
  • git ls-files --modified- shows modified files in the output
  • git ls-files - shows deleted files in the output
  • git ls-files --exclude-standard - adds the standard Git exclusions: .git/info/exclude, .gitignore in each directory, and the user's global exclusion file.
  • grep -vE PATTERN - excludes from the output in accordance with a given pattern
  • paste -sd "|" - concatenates the output by lines with '|' symbol

Upvotes: 3

CodeWizard
CodeWizard

Reputation: 142114

Read the documentation and set up the desired flags.

https://git-scm.com/docs/git-ls-files

You are looking for this:

git ls-files -o --exclude-standard

-c / - -cached
Show cached files in the output (default)

-d / --deleted
Show deleted files in the output

-m / --modified
Show modified files in the output

-o / --others
Show other (i.e. untracked) files in the output

-i / --ignored
Show only ignored files in the output. When showing files in the index, print only those matched by an exclude pattern. When showing "other" files, show only those matched by an exclude pattern.


Another option is to use the git diff with the git-filter

git diff --name-only --diff-filter=A --cached 

--diff-filter=[(A|C|D|M|R|T|U|X|B)…​[*]]

Select only files that are 
  Added (A)   
  Copied (C)   
  Deleted (D)   
  Modified (M)   
  Renamed (R)

Upvotes: 2

Related Questions