user3310334
user3310334

Reputation:

check which files would be tracked by gitignore

I want a list of all the files in my working tree that would be tracked (e.g. would be added by git add -A if every single file in my working tree had been changed) based on the .gitignore files I have written.

Upvotes: 3

Views: 174

Answers (2)

torek
torek

Reputation: 488183

One has to be careful here, as the git add -A flag has changed meanings between Git 1.x and Git 2.x. See Difference between "git add -A" and "git add ." and scroll down to this answer (or click this link) to see what I mean.

For Git 2.x, files that are already tracked—are already in the index—will continue to be in the index. Using -A will ignore any files that have gone missing from the work-tree. (For 1.x, using -A will remove from the index files that have gone missing from the work-tree.)

There are a couple of different things you can do at this point, depending on what you want as a result, largely centering around git ls-files, which:

  • reads the index, so it knows what's in there now
  • optionally reads the work-tree, so that it knows what's there as well
  • optionally reads a specified exclusion file, or the standard ones

The command can be limited to showing:

  • existing tracked files: -c or --cached
  • files that are tracked, yet have been modified in the work-tree: -m or --modified
  • files that are in the work-tree but not in the index (are untracked): -o or --others
  • files that are, or would be, ignored: -i or --ignored

One has to combine these options with --exclude-standard to get git ls-files to obey the same "drop the untracked files" rules as git add. So:

git ls-files --exclude-standard --other

will get you the set of files that are currently untracked, but would be added by git add -A. It won't get you the files that are currently tracked and would also be added by git add -A; to get those, use git ls-files --modified. (To assume that all the files would have been modified, use --cached—but this won't account for work-tree files that have gone missing. If you want to do that, it's considerably trickier, and your best bet is to use git diff, or one of its variants, to compare index and work-tree, using a diff-filter to detect deleted files.)

(Combine the two lists to get everything.)

Upvotes: 3

Romain Valeri
Romain Valeri

Reputation: 21938

git ls-files --exclude-from .gitignore

would have been my best guess, but I tried it on a repo I have here, to no avail :-/ It seems it doesn't take my .gitignore into account.

Maybe not too far from something working?

Upvotes: 1

Related Questions