1

I need a script to process all the staged, modified and untracked files (with honor to ignore).

Since git ls-files --cached lists all the files, either changed or not, in its output, I have to merge the outputs from 2 different commands to get the full list:

# Untracked.
git ls-files --others --exclude-standard

# Modified and cached.
git diff HEAD --name-only

Or include modified in git ls-files

# Modified and untracked
git ls-files --others --modified --exclude-standard

# Cached
git diff --cached --name-only

Can I get the full list with git ls-files only?

Rock Wang
  • 107
  • 9
  • 1
    Short answer: no, not with `git ls-files`. `git status` runs *two* diffs, one HEAD-vs-index and one index-vs-work-tree, with the latter also collecting untracked files (if you use the default or `-uall`), so you can get the same sort of information from one command—`git status`—but not from `git ls-files`. (For programming purposes, you will want `git status --porcelain -uall -z`, or maybe without `-z`, depending on how you intend to process the output.) – torek Dec 29 '16 at 02:23
  • [This](http://stackoverflow.com/questions/40619353/opposite-of-git-info-exclude-including-files-that-are-excluded-by-gitignore) post may be help. You can view different status of files. – mxx Dec 29 '16 at 02:44
  • Addendum: what I mean is, you *can* get what you want if you run `git ls-files` multiple times, but you asked whether you can get it by running it just once. No, because you need to make *two* comparisons: HEAD vs index, and index vs work-tree. – torek Dec 29 '16 at 02:50
  • @torek, sorry for the something inappropriate question. I should have asked "Can I get the full list with one command?", instead of limiting to `git ls-files`. Thanks very much for the insight, now I get everything with `git status -suall --porcelain | awk '{ print $2 }'` – Rock Wang Dec 29 '16 at 02:53
  • You'll probably want something fancier than just `print $2`, eventually: you may need to look at both characters of the first (`$1`) output. Try editing a file, running `git add` (but don't commit yet), then editing the same file again, and see what you get for `git status --porcelain`. Or, create a new file, `git add` it (without committing yet), edit again, and check the status output. – torek Dec 29 '16 at 02:59
  • @torek. Thanks. (Now I start to feeling using 2 commands actually doesn't hurt. :P) – Rock Wang Dec 29 '16 at 03:12
  • @torek wrote: _"Addendum: what I mean is, you can get what you want if you run git ls-files multiple times..."_ ... I'd be really interested to find out more, ie. which `git ls-files` invocations would need to be combined to achieve the desired result through porcelain only... many thanks! – pvandenberk Apr 24 '19 at 13:14
  • @pvandenberk: see Scott Wang's answer; add `git ls-files --exclude-standard --other` for collecting a list of untracked files (after throwing out from that list, any files ignored via `.gitignore` and the like). – torek Apr 24 '19 at 16:58
  • Thanks for getting back to me, @torek, but the missing piece of the puzzle _(unless I'm missing something really trivial here)_ is how to use `git ls-files` to list the modified files in the index, ie. the files with staged/cached changes? – pvandenberk Apr 24 '19 at 18:04
  • `git ls-files --modified` compares the index vs the work-tree to tell you what's *not* staged for commit. You need `git ls-files --staged` (or `git rev-parse`, simpler) to extract the blob hash to compare a file in the index to the blob hash in the `HEAD` commit. (You just need both hashes; if they differ, the files are different.) – torek Apr 24 '19 at 19:09

1 Answers1

-1
  1. for changed files: git ls-files --cached
  2. for modified files: git ls-files --modified
  3. for deleted files: git ls-files --deleted

Hope it helps.

Scott Wang
  • 489
  • 1
  • 7
  • 9